import random
numbers = []
wheel1 = 0
wheel2 = 0
wheel3 = 0
winnings = int(0)
balance = int(50)
def generator(balance):
number1 = random.random()
number2 = random.random()
number3 = random.random()
if number1 < 0.05:
wheel1 = "Cherry"
elif number1 < 0.15:
wheel1 = "Diamond"
elif number1 < 0.30:
wheel1 = "Hearts"
elif number1 < 0.65:
wheel1 = "Spade"
elif number1 < 1:
wheel1 = "Monkey"
if number2 < 0.05:
wheel2 = "Cherry"
elif number2 < 0.15:
wheel2 = "Diamond"
elif number2 < 0.30:
wheel2 = "Hearts"
elif number2 < 0.65:
wheel2 = "Spade"
elif number2 < 1:
wheel2 = "Monkey"
if number3 < 0.05:
wheel3 = "Cherry"
elif number3 < 0.15:
wheel3 = "Diamond"
elif number3 < 0.30:
wheel3 = "Hearts"
elif number3 < 0.65:
wheel3 = "Spade"
elif number3 < 1:
wheel3 = "Monkey"
return wheel1
return wheel2
return wheel3
def win(generator,balance):
generator(balance)
if wheel1 =="Monkey"and wheel2 == "Monkey"and wheel3 == "Monkey":
print "JACKPOT!"
winnings = int(50)
balance + winnings
print 'JACKPOT!'
else:
print 'noice'
winnings = int(10)
balance + winnings
print 'noice'
return balance
print "Welcome to the International Slot Machine"
print ""
print "Balance: $",balance
print ''
spinyn = (raw_input("Would you like to spin? $5 per spin. Enter y or n:\n"))
while True:
if spinyn == "y":
break
elif spinyn == "n":
print "Final Balance: $",balance
print "Thank you for using the International Slot Machine"
raise SystemExit
else:
spinyn = raw_input('\033[31mPlease enter only y or n.\033[0m\n')
spin = (raw_input("Press enter to spin for $5:\n"))
while True:
if spin == '':
balance = balance - 5
if balance <= 0:
print ""
print "Final Balance: $",balance
print "You have run out of money, the game has now ended."
raise SystemExit
print ""
print "\033[34mResult:\033[0m"
print "\033[34m-------\033[0m"
balance = generator(balance)
print ""
print win(generator,balance)
print "New balance:$",balance
print ""
spinagain = (raw_input("Would you like to spin again? Press enter to spin again, type anything to exit.\n"))
while True:
if spinagain == "":
break
else:
print "Final Balance: $",balance
print "Thank you for using the International Slot Machine"
raise SystemExit
else:
spin = (raw_input("Please press enter to spin.\n"))
我很欣赏有关选择随机符号的方法的任何建议,但请保留,因为我只有一个问题。我的问题是:在win功能中,如何识别3轮输出。我已经完成了我认为可行的工作,但它没有,即使我落在3只猴子身上。
欢迎任何其他建议。但请记住,这是最重要的。
非常感谢你。
答案 0 :(得分:0)
你在这里遇到的问题是你的范围概念需要一些帮助。
This answer是一个非常好的开始。
举一个简短的代码示例,让我们这样做:
def generator(balance):
wheel_1 = 'Monkey'
wheel_2 = 'Diamond'
wheel_3 = 'Heart'
return wheel_1, wheel_2, wheel_3
def win(generator):
print wheel_1, wheel_2, wheel_3
win(generator)
您在此处获得的是NameError
,因为wheel_1
(和2&amp; 3)实际上并不存在于win
功能中。它们仅存在于生成器函数中。所以你需要做的是从生成器函数中获取的值,并将它们放在“赢得”的地方。可以看到。实际上,您可以非常轻松地执行此操作,因为我们已经从generator
函数返回值:
# Note: generator was removed as a parameter.
# We don't need it here, because if Python
# can't find the name `generator` it will look
# in the enclosing scope and find the function
# `generator` there.
def win():
# What we want to do is *call* `generator` and
# assign the results to some variables that *are*
# in `win`'s scope
wheel_1, wheel_2, wheel_3 = generator()
print wheel_1, wheel_2, wheel_3
正如您所提到的,您当然可以改进当前代码中的某些内容。您已经注意到generator
功能中的代码看起来几乎完全相同。当你看到代码并且你得到那种感觉时,这就是所谓的代码气味&#34;。当你的(有意识的)头脑看到它所知道的某些代码可以得到改善时会发生什么。在这种特殊情况下,有一个原则叫做 DRY - D R 重新 Y 我们自己。
您可以使用以下内容替换现有代码:
def spin_one():
number = random.random()
if number < 0.05:
result = 'Cherry'
elif number < 0.15:
result = 'Diamond'
elif number < 0.30:
result = 'Hearts'
elif number < 0.65:
result = 'Spade'
else:
result = 'Monkey'
return result
def generator():
return spin_one(), spin_one(), spin_one()
我甚至可能会完全删除生成器调用,只需执行此操作:
if all((spin_one() == 'Monkey' for _ in xrange(3))):
您可以阅读all上的文档。
但是,如果你想获得的不仅仅是赢得很多并赢得一点点,那么你需要保持这些价值观:
wheels = [spin_one() for _ in xrange(3)]
然后你可以这样做:
if all(wheel == 'Monkey' for wheel in wheels):
# Alternatively, wheels.count('Monkey') == 3
print 'Jackpot'
elif wheels.count('Hearts') == 2:
print 'Win'
elif all(wheel == 'Lemon' for wheel in wheels):
print 'You lose'
您可能想要添加任何其他类型的获胜。
<强>提示强>
我不认为这比你当前的快速方法更好或更差,但我过去常常在我的C ++课程中使用它。
def prompt():
choice = raw_input('(Y)es or (N)o: ').lower()
if choice == 'y':
return True
elif choice == 'n':
return False
else:
print '**ERROR** Please input "y" or "n"'
return prompt()
这是一个很好的,简单的方法来处理递归:)