我希望这个老虎机游戏代码循环播放,这样我就可以在不打开和关闭的情况下播放它。我是初学者,所以我可能会遗漏一些东西。我试着让它工作大约一个小时了,我已经尝试了很多解决方案,但他们没有工作
import random
global Cash
global CashLose
global CashWin
Cash = 50
CashLose = 15
CashWin = 20
def SlotMachine ():
global Cash
global CashLose
global RandomNumber
global UserNumber
spin = raw_input (" Press y to spin a slot ")
RandomNumber = random.randrange(1, 7, 1)
if spin == "y":
Cash = Cash - CashLose
UserNumber = raw_input (" pick a number 1-7 ")
print " SPINNING... "
if RandomNumber == UserNumber:
print " You WIn! 20 Dollars Credited "
Cash = Cash + CashWin
print "Cash Ammount" +str(Cash)
else:
print " you lost money, no win! "
print " please spin again! "
print "Cash Ammount" +str(Cash)
答案 0 :(得分:0)
给出线索,把它放在适当的地方
while True:
spin = raw_input(" Press y to spin a slot or n to exit")
if spin == "n":
break
答案 1 :(得分:0)
你的代码真的非常非常简单。在python中,在为其赋值之前不需要声明变量。
做你想做的事的好方法是:
import random
Cash = 50
CashLose = 15
CashWin = 20
while True:
spin = raw_input (" Press y to spin a slot ")
RandomNumber = random.randrange(1, 7, 1)
if spin in ['y', 'Y']:
Cash = Cash - CashLose
UserNumber = raw_input(" pick a number 1-7 ")
print " SPINNING... "
if RandomNumber == UserNumber:
print " You WIn! 20 Dollars Credited "
Cash = Cash + CashWin
print "Cash Ammount", Cash
else:
print " you lost money, no win! "
print " please spin again! "
print "Cash Ammount", Cash
也许您应该考虑阅读python教程和PEP8
答案 2 :(得分:0)
使用itzmeontv建议,您可以使代码循环如下:
import random
Cash = 50
CashLose = 15
CashWin = 20
while True:
spin = raw_input(" Press y to spin a slot or n to exit ")
if spin == "n":
break
RandomNumber = random.randrange(1, 7, 1)
Cash = Cash - CashLose
UserNumber = raw_input (" pick a number 1-7 ")
print " SPINNING... "
if RandomNumber == UserNumber:
print " You WIn! 20 Dollars Credited "
Cash = Cash + CashWin
print "Cash Ammount" +str(Cash)
else:
print " you lost money, no win! "
print " please spin again! "
print "Cash Ammount " +str(Cash)