制作简单、中等、困难三个难度级别的测验
所以基本上我必须编写一个程序,询问用户的难度级别并打印问题,甚至是用户提出的问题的限制。我面临一个问题,我无法打印以下问题:
- 什么是 21+86?
- 什么是92/2?
- 什么是86-70?
number_1 = random.randrange(1, 100000)
number_2 = random.randrange(1, 100000)
当我使用这个命令时,我得到像 19899 + 198846 这样的数字。这是我不想要的。我不知道该怎么办。
import random
print ("Welcome Little Brother!")
#This Educational Software will help you with your arithmetic problem and polish your skills.
score = 0
print("Please make a selection from the following:")
print("P: Practice Math.")
print("S: Show Score.")
print("Q: Quit.")
selection = input("What do you want to do:")
if selection == "S" :
print ("No score found.")
elif selection == "P":
print("What difficult level do you want:")
print("E: Easy.")
print("M: Medium.")
print("H: Hard.")
level = input("Enter Difficulty Level:")
numberofproblem = int(input("How many problems do you want :"))
max_number = numberofproblem
for question_num in range(1, max_number+1):
if level == "E" :
ops = ['+', '-']
elif level == "M" :
ops = ['+', '-', '*']
elif level == "H" :
ops = ['+', '-', '*','/']
else:
print ("Incorrect Menu Key Selected")
number_1 = random.randrange(1, 100000)
number_2 = random.randrange(1, 100000)
operation = random.choice(ops)
maths = round(eval(str(number_1) + operation + str(number_2)),5)
print('\nQuestion number: {}'.format(question_num))
print ("The question is",number_1,operation,number_2)
answer = float(input("What is your answer: "))
if answer == maths:
print("Correct")
score = score + 1
else:
print ("Incorrect. The actual answer is",maths)
So Basically I have to write a program that asks the user for the difficulty level from the user and print questions and even the limit of questions asked from the user. I am facing an issue that I am not able to print questions like these:
- What is 21+86?
- What is 92/2?
- What is 86-70?.
number_1 = random.randrange(1, 100000)
number_2 = random.randrange(1, 100000)
When I use this command I get number like 19899 + 198846. which I don't want. I have no idea what to do.
import random
print ("Welcome Little Brother!")
#This Educational Software will help you with your arithmetic problem and polish your skills.
score = 0
print("Please make a selection from the following:")
print("P: Practice Math.")
print("S: Show Score.")
print("Q: Quit.")
selection = input("What do you want to do:")
if selection == "S" :
print ("No score found.")
elif selection == "P":
print("What difficult level do you want:")
print("E: Easy.")
print("M: Medium.")
print("H: Hard.")
level = input("Enter Difficulty Level:")
numberofproblem = int(input("How many problems do you want :"))
max_number = numberofproblem
for question_num in range(1, max_number+1):
if level == "E" :
ops = ['+', '-']
elif level == "M" :
ops = ['+', '-', '*']
elif level == "H" :
ops = ['+', '-', '*','/']
else:
print ("Incorrect Menu Key Selected")
number_1 = random.randrange(1, 100000)
number_2 = random.randrange(1, 100000)
operation = random.choice(ops)
maths = round(eval(str(number_1) + operation + str(number_2)),5)
print('\nQuestion number: {}'.format(question_num))
print ("The question is",number_1,operation,number_2)
answer = float(input("What is your answer: "))
if answer == maths:
print("Correct")
score = score + 1
else:
print ("Incorrect. The actual answer is",maths)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
根据 documentation
randrange()这些值是在范围
[a,b)
中生成的。这意味着在您的情况下,生成了
1
和100'000
之间的随机值。尝试降低第二个数字,以使生成的任何数字都是可以接受的。100
可能非常适合您的示例。According to the documentation of
randrange()
, the values are generated in the range[a,b)
.This means that in your case a random value between
1
and100'000
is generated. Try to lower the second number so that any number generated is acceptable.100
might be a good fit for your example.您可以制作一个字典来选择难度,并将其分配给一个函数以运行难度测验
You can make a dictionary to choose the difficulty and assign it to a function to run that difficulty quiz