这里的新蟒蛇人
所以我正在尝试制作一个基本的轮盘游戏,有5种选择供玩家选择。我设法让脚本运行没有错误,但是" win_loss"似乎没有注册,或者至少不是if / elif部分。我只能假设我没有在顶部正确识别某些东西。
我很乐意接受一个完整的答案,指导可能会帮助我更多地倾斜。 无论哪种方式,欢迎所有帮助。
import random
red = (1,3,5,7,9,12,14,16,18,21,23,25,27,30,32,34,36)
black = (2,4,6,8,10,11,13,15,17,19,20,22,24,26,28,29,31,33,35)
green = 0
even = (2,4,6,8,10,12,14,16,18,20,22,24,26,28,30,32,34,36)
odd = (1,3,5,7,9,11,13,15,17,19,21,23,25,27,29,31,33,35)
def main():
print('Red = 1')
print('Black = 2')
print('Green = 3')
print('Odd = 4')
print('Even = 5')
print('-=-=-=-=-=-=-=-=-=-=-')
player = int(input('Place your bet, 1-5: '))
print('-=-=-=-=-=-=-=-=-=-=-')
roll()
win_loss()
def roll():
spin = random.randint(1,36)
print('Landed on: ',spin)
print('-=-=-=-=-=-=-=-=-=-=-')
def win_loss():
if (roll) == red:
print('You won $.45')
elif (roll) == black:
print('You won $.45')
elif (roll) == green:
print('You won $5.00')
elif (roll) == even:
print('You won $.45')
elif (roll) == odd:
print('You won $.45')
else:
print('You lost')
main()
答案 0 :(得分:2)
您的功能不会返回任何内容而且他们不接受参数。要让你的win_loss()
函数知道滚动结果是什么,你需要做这样的事情:
def roll():
return random.randint(0,36) # Pointed out by Lallen, you need to include 0
# move the print statements outside the function
def win_loss(roll): # note the parameter here
if (roll) == red:
print('You won $.45')
elif (roll) == black:
print('You won $.45')
elif (roll) == green:
print('You won $5.00')
elif (roll) == even:
print('You won $.45')
elif (roll) == odd:
print('You won $.45')
else:
print('You lost')
在你的剧本中:
result = roll()
win_loss(result)
在此处详细了解functions and parameters。
enter code here
另请注意,您的布尔运算不起作用。您需要测试滚动的结果是否in
您的元组不等于它们。像这样:
# Note the curly braces. As Marius suggested using sets here will make your program a little more efficient
red = {1,3,5,7,9,12,14,16,18,21,23,25,27,30,32,34,36}
black = {2,4,6,8,10,11,13,15,17,19,20,22,24,26,28,29,31,33,35}
green = 0
even = {2,4,6,8,10,12,14,16,18,20,22,24,26,28,30,32,34,36}
odd = {1,3,5,7,9,11,13,15,17,19,21,23,25,27,29,31,33,35}
...
def win_loss(roll): # note the parameter here
if roll in red:
print('You won $.45')
现在效果会更好,因为1 in red
为True
而1 == red
为False
。
您还需要确保玩家下注是赢/输:
def win_loss(roll, player): # note the new player parameter here
if roll in red and player == 1: # note the comparison between the players input and the choices you laid out in the beginning.
print('You won $.45')
额外的布尔表达式将确保滚动位于red
并且玩家下注为红色。必须对所有不同的检查进行此操作。
这些以及评论中列出的要点是一些可以帮助您的事情。我强烈建议你做一些阅读或者一些在线教程,因为你的代码中存在很多潜在的问题。