[英]Program stuck in while loop not printing
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)
Here is my code. 这是我的代码。 I want it to ask the user to press a button, randomly chosen from the set (Q, W, E, R), then print either correct or incorrect, depending on which button they press.
我希望它要求用户按下从设置(Q,W,E,R)中随机选择的按钮,然后打印正确或不正确,具体取决于他们按下的按钮。 I want this to happen 10 times.
我希望这发生10次。 After ten tries it will print their score: 5 for each 'Correct' and -2 for 'Incorrect'.
十次尝试后,它将打印他们的分数:每个'正确'为5,'为'错误'为-2。 Instead I receive this.
相反,我收到了这个。
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)
Regardless of what I enter, it returns neither 'Correct', nor 'Incorrect'. 无论我输入什么,它既不返回'正确',也不返回'不正确'。 It also continues on past 10 and does not show their score.
它也在过去10年继续,并没有显示他们的分数。 There is clearly a problem I am not seeing.
显然我没有看到问题。
My input is in brackets. 我的输入在括号中。
For clarification, this is what I want: 为了澄清,这就是我想要的:
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
In Python indentation is very important . 在Python中缩进非常重要 。
In this code, the x
for the while
loop is never changed as the if
block is on the same indent level as the while
loop. 在此代码中,
while
循环的x
永远不会更改,因为if
块与while
循环位于相同的缩进级别。 So the only looped instruction is result = usertype()
所以唯一的循环指令是
result = usertype()
while x <= 9:
result = usertype()
if result == 'Correct':
x = x+1
y = y+5
Two additional critiques: 另外两个批评:
You are incrementing x
in two places, when it only needs to be done once. 你只需要在两个地方递增
x
,只需要做一次。
while x <= 9:
result = usertype()
if result == 'Correct':
y = y+5
else:
y = y-2
x += 1
Also, since you are looping a fixed number of times, why not ignore incrementing x
and use a for loop, like so: 此外,由于您循环固定次数,为什么不忽略递增
x
并使用for循环,如下所示:
for x in range(10):
result = usertype()
if result == 'Correct':
y = y+5
else:
y = y-2
You need to put the if result == 'Correct':
block under the while x <= 9:
loop where you get the user input, so that it gets evaluate every time. 您需要在
while x <= 9:
循环下放置if result == 'Correct':
block,在那里获得用户输入,以便每次都进行评估。 And you can add the print(result)
to get the correct/incorrect feedback as in your example: 您可以添加
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'
Your main problem was that you had the if/else block in the wrong scope. 你的主要问题是你的if / else块在错误的范围内。 You needed it to be under the
while
block. 你需要它在
while
块下面。 This makes sure that it checks whether the user entered the correct input each time usertype()
is run. 这确保每次运行
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)
Also, you are not printing the value of the variable result. 此外,您不打印变量结果的值。 After evaluating result, add the following:
评估结果后,添加以下内容:
print result
打印结果
Apart from the indentation problem that other people have identified, the code is not particularly idiomatic Python. 除了其他人已经确定的缩进问题之外,代码并不是特别惯用的Python。 The
usertypetest()
function could be: 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
There might be better ways to do this, but this is a little simpler and a little more Pythonic. 可能有更好的方法来做到这一点,但这有点简单,更多一点Pythonic。
I'll also observe that I don't see the parentheses around the input that the example claims to see. 我还会观察到,我没有看到示例声称看到的输入周围的括号。
If you want to see the verdict on each letter, then you need to save the return from usertype()
after all: 如果你想在每个字母上看到判决,那么你需要保存来自
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
Just put if
block under the while
loop. 只需在
while
循环下放置if
块。 Problem solved. 问题解决了。
Try with this code : 试试这段代码:
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)
There are several problems here. 这里有几个问题。
Here is the correct code. 这是正确的代码。
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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.