如何根据random.choice和inpu的结果生成if和elif语句

2024-06-12 20:43:07 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试创建一个游戏,询问你某个角色在书中做了什么,我使用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')

这就是我用来制作游戏的东西


Tags: andoftheinifisrandomhe
3条回答

尝试使用switch case blog和rand integer range(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? ')

您没有将随机卡保存在一个变量中,以便稍后进行计数,

if 'The main character' == random.choice and choice == person1:

random.choice返回的方法对象永远不会与字符串“main character”相同
固定代码:

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')

替代方法:

使用列表索引:

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')

相关问题 更多 >