0

我是在wing ide中使用python的初学者,我正在尝试编写一个老虎机程序。它运行不正常,不断重复 2 条语句,这是我输入“100”时得到的输出:

老虎机 你有 1000 个硬币。按 0 退出,任何其他数字在每次旋转时播放该硬币。100 8 5 3 你输了 100 。你现在有 900 个硬币。按 0 退出,任何其他数字在每次旋转时播放该硬币。你有900个硬币。按 0 退出,任何其他数字在每次旋转时播放该硬币。

- 它重复“按 0 退出,任何其他数字在每次旋转时播放该硬币。” 和“你有()个硬币。”

import random

coins = 1000
wager = 2000

print "Slot Machine"

while coins > 0:
   print "You have",coins, "coins."
   print "Press 0 to exit, any other number to play that coins per spin."
   wager = input("")
   if coins == 0:
       break
   while wager>coins:
       print "Your Wager is greater than the number of coins you have.",
       wager = input("")
   x = random.randint(0,10)
   y = random.randint(0,10)
   z = random.randint(0,10)
   print x,
   print y,
   print z

   if x==y and x==z:
       coins = (coins + wager)*100
       print "You won",wager*100,". You now have" , coins, "coins per spin."
       print "Press 0 to exit, any other number to play that many coins per spin."
   elif x==y or x == z:
       coins = coins + wager*10
       print "You won" ,wager*10,". You now have", coins, "coins."
       print "Press 0 to exit, any other number to play that coins per spin."
   else:
       coins = coins - wager
       print "You lost" ,wager,". You now have", coins, "coins."
       print "Press 0 to exit, any other number to play that coins per spin.",
4

2 回答 2

1

我认为这是您开始在您选择的 IDE 中试用调试器的绝佳机会。尝试单步执行代码以查看执行情况。单步执行代码还将使您有机会看到许多细微差别并在此过程中学习。

最重要的是,不要忘记记录您的问题是什么、解决方案以及您是如何解决的。您只是可能不会说何时需要再次参考它。

于 2014-01-07T17:04:45.727 回答
0

我在这里看到了许多小问题。

首先,只需将这两行移到while语句上方:

print "You have",coins, "coins."
print "Press 0 to exit, any other number to play that coins per spin."

这将停止文本的重复。

print接下来,关于损失,最后一行的末尾有一个逗号。只需删除它,就会打印一个换行符。

我看到的另一个可能是有意的问题是,如果y == z,你没有赢,但我认为你打算让那成为一场胜利。

所以改变这一行:

elif x==y or x == z:

对此:

elif x==y or x == z or y==z:

我看到的最后一件事是当你按 0 时游戏并没有结束。

只需添加这样的一行:

if wager == 0:
    break

这就是我所看到的。这是你的整个程序:

import random

coins = 1000
wager = 2000

print "Slot Machine"

print "You have",coins, "coins."
print "Press 0 to exit, any other number to play that coins per spin."
while coins > 0:

    wager = input("")
    if coins == 0:
        break
    if wager == 0:
        break
    while wager>coins:
        print "Your Wager is greater than the number of coins you have.",
        wager = input("")
    x = random.randint(0,10)
    y = random.randint(0,10)
    z = random.randint(0,10)
    print x,
    print y,
    print z

    if x==y and x==z:
        coins = (coins + wager)*100
        print "You won",wager*100,". You now have" , coins, "coins per spin."
        print "Press 0 to exit, any other number to play that many coins per spin."
    elif x==y or x == z:
        coins = coins + wager*10
        print "You won" ,wager*10,". You now have", coins, "coins."
        print "Press 0 to exit, any other number to play that coins per spin."
    else:
        coins = coins - wager
        print "You lost" ,wager,". You now have", coins, "coins."
        print "Press 0 to exit, any other number to play that coins per spin."
于 2014-01-07T17:19:26.467 回答