【问题标题】:Program stuck in while loop not printing程序卡在while循环不打印
【发布时间】:2014-06-27 04:20:31
【问题描述】:
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。相反,我收到了这个。

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

【问题讨论】:

  • 你有缩进错误

标签: python string random input raw-input


【解决方案1】:

在 Python 中缩进是非常重要的

在此代码中,while 循环的x 永远不会更改,因为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

【讨论】:

  • 尽管引用了9,但原来的循环实际上迭代了10次,所以替换需要range(10)。但是使用range() 肯定更明智。
  • 哎呀。 Fencepost error
【解决方案2】:

您需要将if result == 'Correct': 块放在您获取用户输入的while x &lt;= 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'

【讨论】:

    【解决方案3】:

    您的主要问题是 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)
    

    【讨论】:

      【解决方案4】:

      另外,您没有打印变量结果的值。评估结果后,添加以下内容:

      打印结果

      【讨论】:

        【解决方案5】:

        除了其他人发现的缩进问题之外,代码并不是特别惯用的 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
        

        【讨论】:

        • 感谢您向我展示如何简化它。尽管第一个回答者生成了功能代码,但您会因为简化而获得 +1。另外我只是把括号放在那里来标识输入,它们不在实际输出中。
        【解决方案6】:

        这里有几个问题。

        1. 您需要在用户类型返回之前打印“正确”。
        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)
        

        【讨论】:

          【解决方案7】:

          只需将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)
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2017-04-29
            • 1970-01-01
            • 2021-11-30
            • 2014-12-25
            • 2020-10-05
            相关资源
            最近更新 更多