import random
def usertype():
randletter = random.choice('qwer')
userinput = raw_input('Press '+str(randletter))
if userinput == randletter:
return 'Correct'
else:
return 'Incorrect'
def usertypetest(x,y,result):
while x <= 9:
result = usertype()
if result == 'Correct':
x = x+1
y = y+5
else:
x = x+1
y = y-2
return str(y)+'is your score'
print usertypetest(0,0,usertype)
这是我的代码。我希望它要求用户按下一个按钮,从集合(Q,W,E,R)中随机选择一个按钮,然后打印正确或不正确,具体取决于他们按下哪个按钮。我希望这发生10次。十次尝试后,它会打印他们的分数:每个'正确'为5,'错误'为-2。相反,我收到这个。程序卡在while循环不打印
Press e(e)
Press e(e)
Press w(e)
Press q(e)
Press q(e)
Press q(e)
Press r(e)
Press e(e)
Press w(e)
Press q(e)
Press e(e)
Press e(e)
Press e(e)
Press e(e)
Press q(e)
Press w(e)
Press r(e)
Press w(e)
Press r(e)
Press w(e)
Press r(e)
Press r(e)
无论我输入什么,它都不会返回“正确”或“不正确”。它也在过去10年继续,并没有显示他们的分数。显然有一个问题我没有看到。
我的输入在括号内。
为了澄清,这就是我想要的:
Press q(q)
Correct
Press e(q)
Incorrect
Press w(w)
Correct
Press q(q)
Correct
Press e(eq)
Incorrect
Press e(e)
Correct
Press q(q)
Correct
Press q(r)
Incorrect
Press w(w)
Correct
Press r(r)
Correct
29 is your score
你有压痕错误 – Fabricator