我是在wing ide中使用python的初学者,我正在尝试编写老虎机程序。它运行不正常,它不断重复2个语句,这是我输入“100”时得到的输出:
老虎机 你有1000个硬币。 按0退出,任何其他数字每次旋转播放该硬币。 100 8 5 3 你失去了100。你现在有900个硬币。 按0退出,任何其他数字每次旋转播放该硬币。你有900个硬币。 按0退出,任何其他数字每次旋转播放该硬币。
-it重复“按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.",
答案 0 :(得分:1)
我认为这是一个很好的机会让您开始在您选择的IDE中试用调试器。尝试单步执行代码以查看执行情况。单步执行代码还可以让您有机会看到许多细微差别并一路学习。
最重要的是,不要忘记记录您的问题,解决方案以及您是如何达成的。你可能不会说什么时候你需要再次参考它。
答案 1 :(得分: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."