Я пытаюсь создать игру, которая спрашивает вас, что определенный персонаж делает в книге, и я использую 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
santiago 28 Июн 2019 в 20:07

3 ответа

Лучший ответ

Вы не сохраняете случайную карту в переменной, чтобы иметь возможность подсчитать ее позже,

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')
0
waynetech 28 Июн 2019 в 17:20

Альтернативные методы:

Используя индекс списка:

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')
0
Ricky Kim 28 Июн 2019 в 17:27

Попробуйте переключить регистр блога и диапазон целых чисел 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? ')
0
İsa GİRİŞKEN 28 Июн 2019 в 17:15