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,正确&#39;和-2为&#39;不正确&#39;。相反,我收到了这个。
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
答案 0 :(得分:3)
在Python缩进中,非常重要。
在此代码中,x
循环的while
永远不会更改,因为if
块与while
循环位于同一缩进级别。所以唯一的循环指令是result = usertype()
while x <= 9:
result = usertype()
if result == 'Correct':
x = x+1
y = y+5
另外两个批评:
你只需要在两个地方递增x
,只需要做一次。
while x <= 9:
result = usertype()
if result == 'Correct':
y = y+5
else:
y = y-2
x += 1
此外,由于您循环固定次数,为什么不忽略递增x
并使用for循环,如下所示:
for x in range(10):
result = usertype()
if result == 'Correct':
y = y+5
else:
y = y-2
答案 1 :(得分:2)
您需要将if result == 'Correct':
块放在获得用户输入的while x <= 9:
循环下,以便每次都进行评估。您可以添加print(result)
以获取正确/错误的反馈,如示例所示:
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
print(result)
return str(y)+'is your score'
答案 2 :(得分:1)
你的主要问题是你的if / else块在错误的范围内。你需要它在while
块之下。这样可以确保每次运行usertype()
时都会检查用户是否输入了正确的输入。
import random
moves = 0
score = 0
def usertype():
randletter = random.choice('qwer')
userinput = raw_input('Press '+str(randletter))
if userinput == randletter:
return True
else:
return False
def usertypetest(moves, score):
while moves < 10:
result = usertype()
moves = moves + 1
if result:
score = score + 5
else:
score = score - 2
return str(score) + ' is your score'
print usertypetest(moves, score)
答案 3 :(得分:1)
此外,您不打印变量结果的值。评估结果后,添加以下内容:
打印结果
答案 4 :(得分:1)
除了其他人已经确定的缩进问题之外,代码并不是特别惯用的Python。 usertypetest()
功能可以是:
def usertypetest(x,y,result):
for x in range(10):
if usertype() == 'Correct':
y = y + 5
else:
y = y - 2
return '%d is your score' % y
可能有更好的方法可以做到这一点,但这有点简单,也有点Pythonic。
我还会注意到,我没有看到示例声称看到的输入附近的括号。
如果你想看到每个字母的判决,那么你需要保存usertype()
的回报:
def usertypetest(x,y,result):
for x in range(10):
result = usertype()
print result
if result == 'Correct':
y = y + 5
else:
y = y - 2
return '%d is your score' % y
答案 5 :(得分:0)
只需将if
块放在while
循环下即可。
问题解决了。
尝试使用此代码:
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)
答案 6 :(得分:0)
这里有几个问题。
这是正确的代码。
import random
def usertype():
randletter = random.choice('qwer')
userinput = raw_input('Press '+str(randletter))
if userinput == randletter:
print 'Correct'
return 'Correct'
else:
print 'Incorrect'
return 'Incorrect'
def usertypetest(x,y):
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)