找到可以检查特定算术答案是否正确的东西
Finding something that checks if a particular Arithmetic answer is correct
我想知道是否有一个命令可以计算出您提出的数学问题并将其与用户编写的答案进行比较?显然错了就输出错,对了就输出对。这是我的代码:
import time
person=input('Hello there, what is your name? ')
print('Hello',person,'today you will test a maths quiz which is 10 questions')
time.sleep(1)
print('Good luck here is your first question:')
UserScore=0
UserWrong=0
x=0
while x<10:
import random
Ran=random.randint(1, 10)
dom=random.randint(1, 10)
Operators=[ 'plus', 'minus', 'times']
op = random.choice(Operators)
AnswerOne=input('What is '+str(Ran) +' '+str(op) +' '+str(dom) +'? ')
if int(AnswerOne) == Ran + dom:
print('Correct!')
UserScore= UserScore + 1
elif int(AnswerOne) == Ran - dom:
print('Correct!')
UserScore= UserScore + 1
elif int(AnswerOne) == Ran * dom:
print('Correct!')
UserScore= UserScore + 1
else:
print('You are wrong! Better look next time :D')
UserWrong= UserWrong +1
x=x+1
print('You got '+str(UserScore) +' right and '+str(UserWrong) +' wrong')
编辑: 好吧,我把这个切下来了一点。
我有一个问题基础 class 和四个子问题class:乘法、除法、加法和减法。
每次您创建一个 Question 对象时,它都会用随机值初始化自己。然后您可以请求一个问题字符串和相关的答案值。
from random import choice, randint
class Question: # base class
def __init__(self, difficulty=20):
self.a = randint(1, difficulty)
self.b = randint(1, difficulty)
def question(self):
raise NotImplemented
def answer(self):
raise NotImplemented
class Multiplication(Question):
def question(self):
return "What is {} * {}? ".format(self.a, self.b)
def answer(self):
return self.a * self.b
class Division(Question):
def question(self):
# invert the question to ensure a nice answer value
return "What is {} / {}? ".format(self.a * self.b, self.a)
def answer(self):
return self.b
class Addition(Question):
def question(self):
return "What is {} + {}? ".format(self.a, self.b)
def answer(self):
return self.a + self.b
class Subtraction(Question):
def question(self):
# invert the question to ensure a nice answer value
return "What is {} - {}? ".format(self.a + self.b, self.a)
def answer(self):
return self.b
然后我使用 classes 实现一个提出问题并标记结果的函数,
def do_question(qtypes=[Multiplication, Division, Addition, Subtraction]):
# pick a random question-type
# and create a question of that type
q = choice(qtypes)()
# prompt user for their answer
guess = get_int(q.question())
# mark their answer
if guess == q.answer():
print("Correct!")
return True
else:
print("Sorry, the right answer was {}".format(q.answer()))
return False
和一个函数,它提出一定数量的问题和 returns 总分,
def do_quiz(num_questions=10):
return sum(do_question() for i in range(1, num_questions + 1))
然后我们进行测验并显示结果:
def main():
correct = do_quiz()
print("\nYou solved {} of 10 questions".format(correct))
if __name__ == "__main__":
main()
并且,当 运行 时,这看起来像
What is 8 * 2? 16
Correct!
What is 1 + 3? 4
Correct!
What is 27 - 12? 15
Correct!
What is 17 + 1? 19
Sorry, the right answer was 18
What is 4 / 1? 4
Correct!
What is 19 - 14? 5
Correct!
What is 36 / 3? 12
Correct!
What is 12 * 7? 84
Correct!
What is 48 / 4? 12
Correct!
What is 26 - 8? 18
Correct!
You solved 9 of 10 questions
你做的是可行的,只是稍微调整一下条件。您当前说的是 "if the user's answer is a+b, then its right" - 即使问题是 "what is a times b"。因此,您还需要检查操作是否匹配。你可以这样做:
if answer == ran+dom and op == 'plus':
# correct
其他操作也类似。
您还可以通过使用字典而不是列表来简化操作 - 键是当前字符串,值是执行正确操作的函数,例如运算符中的那些模块,所以:
operations = { "plus": operator.add,
"times": operator.mul,
"minus": operator.sub
}
这让您可以简化为 一个 条件 - 您可以从字典中提取适当的检查功能并测试它是否给出与用户相同的答案:
if operations[op](ran, dom) == answer:
# correct
这涵盖了所有三个分支,您以后决定添加更多分支。
我想知道是否有一个命令可以计算出您提出的数学问题并将其与用户编写的答案进行比较?显然错了就输出错,对了就输出对。这是我的代码:
import time
person=input('Hello there, what is your name? ')
print('Hello',person,'today you will test a maths quiz which is 10 questions')
time.sleep(1)
print('Good luck here is your first question:')
UserScore=0
UserWrong=0
x=0
while x<10:
import random
Ran=random.randint(1, 10)
dom=random.randint(1, 10)
Operators=[ 'plus', 'minus', 'times']
op = random.choice(Operators)
AnswerOne=input('What is '+str(Ran) +' '+str(op) +' '+str(dom) +'? ')
if int(AnswerOne) == Ran + dom:
print('Correct!')
UserScore= UserScore + 1
elif int(AnswerOne) == Ran - dom:
print('Correct!')
UserScore= UserScore + 1
elif int(AnswerOne) == Ran * dom:
print('Correct!')
UserScore= UserScore + 1
else:
print('You are wrong! Better look next time :D')
UserWrong= UserWrong +1
x=x+1
print('You got '+str(UserScore) +' right and '+str(UserWrong) +' wrong')
编辑: 好吧,我把这个切下来了一点。
我有一个问题基础 class 和四个子问题class:乘法、除法、加法和减法。
每次您创建一个 Question 对象时,它都会用随机值初始化自己。然后您可以请求一个问题字符串和相关的答案值。
from random import choice, randint
class Question: # base class
def __init__(self, difficulty=20):
self.a = randint(1, difficulty)
self.b = randint(1, difficulty)
def question(self):
raise NotImplemented
def answer(self):
raise NotImplemented
class Multiplication(Question):
def question(self):
return "What is {} * {}? ".format(self.a, self.b)
def answer(self):
return self.a * self.b
class Division(Question):
def question(self):
# invert the question to ensure a nice answer value
return "What is {} / {}? ".format(self.a * self.b, self.a)
def answer(self):
return self.b
class Addition(Question):
def question(self):
return "What is {} + {}? ".format(self.a, self.b)
def answer(self):
return self.a + self.b
class Subtraction(Question):
def question(self):
# invert the question to ensure a nice answer value
return "What is {} - {}? ".format(self.a + self.b, self.a)
def answer(self):
return self.b
然后我使用 classes 实现一个提出问题并标记结果的函数,
def do_question(qtypes=[Multiplication, Division, Addition, Subtraction]):
# pick a random question-type
# and create a question of that type
q = choice(qtypes)()
# prompt user for their answer
guess = get_int(q.question())
# mark their answer
if guess == q.answer():
print("Correct!")
return True
else:
print("Sorry, the right answer was {}".format(q.answer()))
return False
和一个函数,它提出一定数量的问题和 returns 总分,
def do_quiz(num_questions=10):
return sum(do_question() for i in range(1, num_questions + 1))
然后我们进行测验并显示结果:
def main():
correct = do_quiz()
print("\nYou solved {} of 10 questions".format(correct))
if __name__ == "__main__":
main()
并且,当 运行 时,这看起来像
What is 8 * 2? 16
Correct!
What is 1 + 3? 4
Correct!
What is 27 - 12? 15
Correct!
What is 17 + 1? 19
Sorry, the right answer was 18
What is 4 / 1? 4
Correct!
What is 19 - 14? 5
Correct!
What is 36 / 3? 12
Correct!
What is 12 * 7? 84
Correct!
What is 48 / 4? 12
Correct!
What is 26 - 8? 18
Correct!
You solved 9 of 10 questions
你做的是可行的,只是稍微调整一下条件。您当前说的是 "if the user's answer is a+b, then its right" - 即使问题是 "what is a times b"。因此,您还需要检查操作是否匹配。你可以这样做:
if answer == ran+dom and op == 'plus':
# correct
其他操作也类似。
您还可以通过使用字典而不是列表来简化操作 - 键是当前字符串,值是执行正确操作的函数,例如运算符中的那些模块,所以:
operations = { "plus": operator.add,
"times": operator.mul,
"minus": operator.sub
}
这让您可以简化为 一个 条件 - 您可以从字典中提取适当的检查功能并测试它是否给出与用户相同的答案:
if operations[op](ran, dom) == answer:
# correct
这涵盖了所有三个分支,您以后决定添加更多分支。