我正在尝试创建一个简单的轮盘游戏,作为python的初学者,我遇到了一些问题。
到目前为止,我有下面的代码,并创建了一些函数,试图让用户输入激活不同的功能。我已经在红色/黑色和奇数/偶数函数中创建了一些测试语句,但是,无论输入是什么,我只获得红色/黑色函数的输出(如果输入红色则为10,对于任何其他输入则为20)。任何帮助表示赞赏!
import random, time
# the spin and result of the roulette wheel
def spin():
print("Wheel is spinning...")
time.sleep(3)
land_number = random.randint(0,36)
print (land_number)
def number_chosen(x):
pass
def red_black_chosen(chosen_bet):
if chosen_bet == 'red':
red = 10
print(red)
else:
black = 20
print(black)
def odd_even_chosen(chosen_bet):
if chosen_bet == 'odd':
odd = 'qwe'
print(odd)
else:
even = 'abc'
print(even)
def high_low_chosen(chosen_bet):
pass
def dozen_chosen(chosen_bet):
pass
#choice of bet
def betters_choice():
print("""How would you like to bet? \n
Choose a number
Choose red or black
Choose odd or even
Choose low(1-18) or high(19-36
Choose 1st dozen(1-12), 2nd dozen(13-24) or 3rd dozen(25-
36)""")
global chosen_bet
chosen_bet = input()
if type(chosen_bet) == int:
if chosen_bet < 0:
print("Bet must be between 0 and 36")
elif chosen_bet > 36:
print("Bet must be between 0 and 36")
else:
number_chosen(chosen_bet)
elif chosen_bet == 'red' or 'black':
red_black_chosen(chosen_bet)
elif chosen_bet == 'odd' or 'even':
odd_even_chosen(chosen_bet)
elif chosen_bet == 'low' or 'high':
high_low_chosen(chosen_bet)
elif chosen_bet in ['1st dozen', '2nd dozen', '3rd dozen']:
dozen_chosen(chosen_bet)
else:
print("Incorrect bet chosen")
betters_choice()
betters_choice()
答案 0 :(得分:1)
这是因为if函数不接受这样的多个条件。它们应该单独说明:
import random, time
# the spin and result of the roulette wheel
def spin():
print("Wheel is spinning...")
time.sleep(3)
land_number = random.randint(0,36)
print (land_number)
def number_chosen(x):
pass
def red_black_chosen(chosen_bet):
if chosen_bet == 'red':
red = 10
print(red)
else:
black = 20
print(black)
def odd_even_chosen(chosen_bet):
if chosen_bet == 'odd':
odd = 'qwe'
print(odd)
else:
even = 'abc'
print(even)
def high_low_chosen(chosen_bet):
pass
def dozen_chosen(chosen_bet):
pass
#choice of bet
def betters_choice():
print("""How would you like to bet? \n
Choose a number
Choose red or black
Choose odd or even
Choose low(1-18) or high(19-36
Choose 1st dozen(1-12), 2nd dozen(13-24) or 3rd dozen(25-
36)""")
global chosen_bet
chosen_bet = input()
if type(chosen_bet) == int:
if chosen_bet < 0:
print("Bet must be between 0 and 36")
elif chosen_bet > 36:
print("Bet must be between 0 and 36")
else:
number_chosen(chosen_bet)
elif chosen_bet == 'red' or chosen_bet =='black':
red_black_chosen(chosen_bet)
elif chosen_bet == 'odd' or chosen_bet =='even':
odd_even_chosen(chosen_bet)
elif chosen_bet == 'low' or chosen_bet =='high':
high_low_chosen(chosen_bet)
elif chosen_bet in ['1st dozen', '2nd dozen', '3rd dozen']:
dozen_chosen(chosen_bet)
else:
print("Incorrect bet chosen")
betters_choice()
betters_choice()
答案 1 :(得分:1)
addEventListener('load')
始终属于chosen_bet
类型,因为input始终返回字符串。
首先尝试在str
上致电isdigit()
。
chosen_bet
我还修复了if chosen_bet.isdigit():
# then check values and such
if int(chosen_bet) < 0:
elif chosen_bet == 'red' or chosen_bet == 'black'
# rest of your code
声明中的错误。您需要对elif
两边的chosen_bet
进行比较。