我正在尝试制作代数节目,但即使我输入了正确的答案,也会打印错误的
x= random.randrange(1, 100)
x2 = random.randrange(1, 100)
answer = input('if x + ' + str(x2) + ' = ' + str(x + x2) + ' what is x equal to? ')
ranswer = x + x2
if answer == ranswer:
print('correct')
else:
print('incorrect')
NVM GUYS我只是愚蠢 应该做ranswer = x :P
答案 0 :(得分:1)
x= random.randrange(1, 100)
x2 = random.randrange(1, 100)
answer = input('if x + ' + str(x2) + ' = ' + str(x + x2) + ' what is x equal to? ')
if int(answer) == x:
print('correct')
else:
print('incorrect')
正如马利克所说,你的逻辑存在问题。我解决了这个问题,并将answer
变量转换为整数,以便正确比较。
答案 1 :(得分:1)
监控你的代码(python 2.7)
#exam_plus.py
import random
x= random.randrange(1, 100)
x2 = random.randrange(1, 100)
answer = raw_input('if x + ' + str(x2) + ' = ' + str(x + x2) + ' what is x equal to? ')
ranswer = x + x2
print "answer", answer
print "correct answer", ranswer #it is obviously not correct, i just put it to show what your program expects
if int(answer) == ranswer:
print'correct'
else:
print 'incorrect'
运行它
$ python exam_plus.py
if x + 52 = 55 what is x equal to? 3
answer 3
correct answer 55
incorrect
$ python exam_plus.py
if x + 81 = 88 what is x equal to? 7
answer 7
correct answer 88
incorrect
$ python exam_plus.py
if x + 3 = 71 what is x equal to? 68
answer 68
correct answer 71
incorrect
这意味着您的问题逻辑不正确 - 您应该编辑ranswer=x
,这将导致
$ python exam_plus.py
if x + 12 = 72 what is x equal to? 60
answer 60
correct answer 60
correct
$ python exam_plus.py
if x + 24 = 61 what is x equal to? 37
answer 37
correct answer 37
correct
$ python exam_plus.py
if x + 88 = 151 what is x equal to? 63
answer 63
correct answer 63
correct