我正在尝试根据制作的骰子打印某些字符串。
我尝试为哪些掷骰子获得哪个事件列出清单,但是即使那样,没有任何事件仅打印出我掷出的掷骰子。
import random
def dice_roll():
d20_roll = random.randint(1,20)
print("You rolled " +str(d20_roll))
def dice_choice():
event = str(d20_roll)
bad_list = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10',
'11', '12']
good_list = ['13', '14', '15', '16', '17', '18']
gag_list = ['19', '20']
if event == bad_list:
print('bad_list stuff')
elif event == good_list:
print('good_list stuff')
else:
if event == print gag_list:
print("gag_list stuff")
dice_choice()
dice_roll()
我希望输出是随机滚动将产生的三个选项中的任何一个。 我收到的只是dice_roll本身的结果,没有选择。
答案 0 :(得分:1)
首先,检查您的缩进,您对dice_choice()
的调用似乎在dice_choice()
内部,其次,测试if event == print gag_list:
中存在语法错误,第三,您正在测试是否一个字符串等于一个列表,相反,您应该测试该字符串是否在列表中,您的代码应如下所示:
import random
def dice_roll():
d20_roll = random.randint(1,20)
print("You rolled " +str(d20_roll))
def dice_choice():
event = str(d20_roll)
bad_list = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12']
good_list = ['13', '14', '15', '16', '17', '18']
gag_list = ['19', '20']
if event in bad_list: # check using `in` not `===`
print('bad_list stuff')
elif event in good_list:
print('good_list stuff')
elif event == gag_list:
print("gag_list stuff")
dice_choice()
dice_roll()
示例输出:
You rolled 11
bad_list stuff