【问题标题】:Infinite Loop Error in PythonPython中的无限循环错误
【发布时间】:2014-01-24 00:11:36
【问题描述】:

我在这里有这个短代码。但它没有使用 python 在wing ide 4.1 中打印,因为它是一个无限循环。关于我可以添加什么或如何修复它以便打印的任何想法?

import random
coins = 1000
wager = 2000
while ((coins>0) and (wager!= 0)):
x = random.randint(0,10)
y = random.randint(0,10)
z = random.randint(0,10)
print x,
print y,
print z

【问题讨论】:

  • 缩进在 Python 中很重要;您能否正确缩进while 之后的行以反映您的代码?

标签: python python-2.7 infinite-loop


【解决方案1】:

您的代码永远不会改变coins wager,因此while 条件总是为真:

while ((coins>0) and (wager!= 0)):
    # code that doesn't touch coins or wager

也许您还打算从coinswager 中减去xyz 之一?

至于为什么代码在 Wing IDE 中不打印;这完全取决于你的缩进。如果print 语句不是循环的一部分,则它们永远不会到达并且永远不会执行。尝试创建一个不会无限运行的循环。

【讨论】:

    【解决方案2】:

    您发布的代码只会选择 3 个伪随机数,直到时间结束。您必须添加一些赢/输条件。到目前为止,x、y 和 z 只是数字。如果您想制作赌博游戏,您必须添加一些胜利条件,例如:

    if x + y + z > 10
    

    只是一个示例,但您的程序需要能够判断玩家是否获胜。然后它需要更改玩家的总金额并要求新的赌注。您可能还想添加逻辑以确保玩家不能下注超过他们的赌注。

    import random
    coins = 1000
    wager = 0
    while True: #main loop
        print('you have {} coins'.format(coins))
        if coins == 0: #stops the game if the player is out of money
            print('You are out of money! Scram, deadbeat!')
            break
        while wager > coins or wager == 0: #loops until player enters a non-zero wager that is less then the total amount of coins
            wager = int(input('Please enter your bet (enter -1 to exit): '))
        if wager < 0: # exits the game if the player enters a negative
            break
        print('All bets are in!') 
        x = random.randint(0,10)
        y = random.randint(0,10)
        z = random.randint(0,10)
        print(x,y,z) #displays all the random ints
        if x + y +z > 10: #victory condition, adds coins for win
            print('You win! You won {} coins.'.format(wager))
            coins += wager
        else: #loss and deduct coins
            print('You lost! You lose {} coins'.format(wager))
            coins -= wager
        wager = 0 # sets wager back to 0 so our while loop for the wager validation will work
    

    【讨论】:

      【解决方案3】:

      因为在您的 while 循环中没有修改您的中断条件。

      在您的情况下,中断条件是wager is not 0coins &gt; 0,因此您必须修改代码中的coinswager 变量,例如

      import random
      coins = 1000; wager = 2000
      while coins > 0 and wager is not 0:
        x,y,z = [random.randint(0,10)]*3
        print x,y,z
        wager-= 1000
        coins-= 100
      

      由于@martijn 缩进在python中很重要,请参阅http://en.wikipedia.org/wiki/Python_syntax_and_semantics#Indentation

      for _ in range(10):
        x = 'hello world'
        print x
      

      [出]:

      hello world
      hello world
      hello world
      hello world
      hello world
      hello world
      hello world
      hello world
      hello world
      hello world
      

      print x 没有缩进时:

      for _ in range(10):
        x = 'hello world'
      print x
      

      [出]:

      hello world
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-10-12
        • 2013-12-24
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多