我正在尝试创建一个游戏,询问你某个角色在书中做了什么,我使用random.choice和if and elif语句来实现这一点,但是当我键入输入来回答问题时,输出总是错误的,即使我回答了问题
我试着说如果:
if 'The main character' in card_list and choice == person1:
print("correct")
然而,对于每一个陈述,即使我输入了错误的答案,结果总是正确的
import random
card1 = 'The main character'
card2 = 'He brings aleem on holiday'
card3 = 'Dies in the middle of the book'
card4 = "She takes care of aleem while aleem's dad is in Ethiopia"
card5 = 'He helps take care of aleem with Miss Fitzgerald'
card6 = "He is in charge of aleem's court case"
person1 = 'aleem'
person2 = "aleem's dad"
person3 = "aleem's mum"
person4 = 'Miss Fitzgerald'
person5 = 'Mr Fitzgerald'
person6 = 'Nicolas'
card_list = [card1, card2, card3, card4, card5, card6]
print(random.choice(card_list))
choice = input('Who is this? ')
if 'The main character' == random.choice and choice == person1:
print("correct")
elif 'He brings aleem on holiday' == random.choice and choice ==
person2:
print('correct')
elif 'Dies in the middle of the book' == random.choice and choice ==
person3:
print('correct')
elif "She takes care of aleem while aleem's dad is in Ethiopia" ==
random.choice and choice == person4:
print('correct')
elif 'He helps take care of aleem with Miss Fitzgerald' ==
random.choice and choice == person5:
print('correct')
elif "He is in charge of aleem's court case" == random.choice and
choice == person6:
print('correct')
else:
print('wrong')
这就是我用来制作游戏的东西
尝试使用switch case blog和rand integer range(0,6)
您没有将随机卡保存在一个变量中,以便稍后进行计数,
random.choice返回的方法对象永远不会与字符串“main character”相同
固定代码:
替代方法:
使用列表索引:
使用字典:
相关问题 更多 >
编程相关推荐