2014-06-27 88 views
3
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 
+2

你有压痕错误 – Fabricator

回答

3

在Python缩进非常重要

在该代码中,xwhile循环从未改变作为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

尽管参考9,原始循环实际上迭代10次,所以更换需要'范围(10)'。但是使用'range()'确实更合理。 –

+0

大家好。 [Fencepost错误](http://en.wikipedia.org/wiki/Off-by-one_error#Fencepost_error) –

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' 
0

只要把ifwhile循环下。 问题解决了。

尝试使用此代码:

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) 
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) 
1

此外,不打印变量的结果的值。评估结果后,添加以下内容:

打印结果

0

这里有几个问题。

  1. 您需要在usertype返回前打印'正确'。
  2. 你不需要把'结果'放在usertypetest中。
  3. 把if ... else放在usertypetest的循环中。
  4. 更改上次打印。

这里是正确的代码。

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) 
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 

可能有更好的方式来做到这一点,但这是稍微简单一些,多一些Python化。

我还会观察到,我没有看到示例声称看到的输入周围的括号。

如果你想看到每个字母的判决,那么你需要拯救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 
+0

感谢您向我展示如何简化它。尽管第一个回答者生成了功能性代码,但您为了简化而获得+1。此外,我只是把括号放在那里以确定输入,它们不在实际输出中。 – Bretsky