我得到错误:
randint = randint(1, 2)
TypeError: 'int' object is not callable
这是我的代码。我不知道是什么原因导致了这种情况的发生,这是否与循环两次有关,因为它允许我回答第一个问题,然后失败:截图。
from random import randint
score = 0
print('nnnnyour score right now is ', score, 'if it drops below zero you lose')
def make_question(num, italian, answer):
global score
if randint == int(num):
a = input('nWhat does ' + str(italian) + ' mean? ')
if a == str(answer):
score += 1
print('ncorrect your score is: ', score)
else:
score -= 1
print('nincorrect your score is now: ', score)
while True:
randint = randint(1, 2)
make_question(1, 'ripetere', 'repeat')
make_question(2, 'puoi/potere ripetere', 'can you repeat that')
第一次:
randint = randint(1, 2)
randint
为random
模块的函数,通话正常。然后你把它换成1
或2
,现在它就只剩下int
了。为您生成的值使用不同的名称(例如,whichquestion = randint(1, 2)
并将if randint == int(num):
替换为if whichquestion == int(num):
),这样您就不会丢弃您需要的函数的句柄。