我正在尝试创建一个游戏,询问您书中某个角色的行为,并且我使用random.choice以及if和elif语句来执行此操作,但是当我输入内容以回答问题时,输出是即使我回答了赖特问题,总是错的。
我试图做出if语句:
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')
这就是我用来创建游戏的东西。
答案 0 :(得分:0)
尝试切换案例博客和rand整数范围(0,6)
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))
def numbers_to_strings(argument):
switcher = {
0: "card1",
1: "card2",
2: "card3"...
}
return switcher.get(argument, "nothing")
choice = input('Who is this? ')
答案 1 :(得分:0)
您没有将随机卡保存在变量中,以便以后可以对它进行计数,
if 'The main character' == random.choice and choice == person1:
random.choice返回永远不会与字符串“主字符”相同的方法对象
固定代码:
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]
random_card = random.choice(card_list)
print(random_card)
choice = input('Who is this? ')
if 'The main character' == random_card and choice == person1:
print("correct")
elif 'He brings aleem on holiday' == random_card and choice == person2:
print('correct')
elif 'Dies in the middle of the book' == random_card and choice == person3:
print('correct')
elif "She takes care of aleem while aleem's dad is in Ethiopia" == random_card and choice == person4:
print('correct')
elif 'He helps take care of aleem with Miss Fitzgerald' == random_card and choice == person5:
print('correct')
elif "He is in charge of aleem's court case" == random_card and choice == person6:
print('correct')
else:
print('wrong')
答案 2 :(得分:0)
替代方法:
使用列表的索引:
card_list = [card1, card2, card3, card4, card5, card6]
person_list= [person1, person2, person3, person4, person5, person6]
rand_choice=random.choice(card_list)
print(rand_choice)
choice = input('Who is this? ')
if choice in person_list and person_list.index(choice)==card_list.index(rand_choice):
print('correct')
else:
print('wrong')
使用字典:
card_list = [card1, card2, card3, card4, card5, card6]
person_list= [person1, person2, person3, person4, person5, person6]
d = {p:c for p,c in zip(person_list, card_list)}
rand_choice=random.choice(card_list)
print(rand_choice)
choice = input('Who is this? ')
if choice in d and rand_choice==d.get(choice):
print('correct')
else:
print('wrong')