简体   繁体   中英

Python: Random trivia trivia question/answer, in parameters

I am new to learning python and want to run bit of a trivia. Basically, I want to ask a random question from a list and then using an 'in' operator, figure if the user input of Y/N is correct or not. I'm stuck with determining how to check whether it is correct or not. Maybe my (incorrect) code can explain better.

import random

Players = ['Patrice Evra', 'Rio Ferdinand', 'Sergio Ramos', 'Gerard Pique']
Clubs = ['Manchester United', 'Nice', 'Monaco', 'Marseille', 'West Ham United', 'Sevilla', 'Real Madrid', 'Barcelona']
Ramos = ['Sevilla', 'Real Madrid']
Evra = ['Manchester United', 'Nice', 'Monaco', 'Marseille', 'West Ham United']
Ferdinand = ['Leeds United', 'Manchester United']
Pique = ['Barcelona', 'Manchester United']
print('Did ' + random.choice(Players) + ' play for ' + random.choice(Clubs) + ' ? Y/N')
answer = input()

This is where I'm stuck and not even sure if this is the right way to go about this. Thanks for the help.

Most importantly you have to save your random choice so you can verify it later. So you should asign two variables before:

player = random.choice(Players)
club = random.choice(Clubs)
print('Did ' + player + ' play for ' + club + ' ? Y/N')

Then you can verify that yourself via if statements. But a faster (more complex) way would be:

did_play = club in [Evra, Ferdinand, Ramos, Pique][Players.index(player)]

It would be better to store the variables Evra , Ferdinand , Ramos and Pique differently, but for now that should to the trick.

A better way to store those variables would be in a dictionary like such:

player_clubs = {
"Patrice Evra": ['Manchester United', 'Nice', 'Monaco', 'Marseille', 'West Ham United'],
"Rio Ferdinand": ['Leeds United', 'Manchester United'],
"Sergio Ramos": ['Sevilla', 'Real Madrid'],
"Gerard Pique": ['Barcelona', 'Manchester United']
}

That way you can easier check if they played in a certain club like:

did_play = club in player_clubs[player]

You need to associate a player with a club. This is best done with a dictionary, which I call clubs here. The keys of this dictionary are the club names and the values are a list of players in each club. Also, when you ask for user input, you should put your prompt question within the argument of the input.

import random
players = ['Patrice Evra', 'Rio Ferdinand', 'Sergio Ramos', 'Gerard Pique']
clubs = {'Manchester United':['Patrice Evra', 'Rio Ferdinand', 'Gerard Pique'],
   'Nice':['Patrice Evra'],
   'Monaco':['Patrice Evra'],
   'Marseille':['Patrice Evra'],
   'West Ham United':['Patrice Evra'],
   'Sevilla':['Sergio Ramos'],
   'Real Madrid':['Sergio Ramos'],
   'Barcelona':['Gerard Pique'],
   'Leeds Nations': ['Rio Ferdinand']}

player = random.choice(players)
club = random.choice(list(clubs.keys()))
answer = input(f'Did {player} play for {club}? y/n: ')
if answer == "y" and player in clubs[club]\
        or answer == "n" and player not in clubs[club]:
    print("Correct!")
else:
    print("wrong")

Here is way to generate the questions out of one dictionary. I realize some of the code might be a bit confusing if you have just begun coding, but it will provide you some interesting stuff to look into.

import random

#Store all clubs per player in a dictionary
games_dict = {"Sergio Ramos": ['Sevilla', 'Real Madrid'], "Patrice Evra": ['Manchester United', 'Nice', 'Monaco', 'Marseille', 'West Ham United'], 
"Rio Ferdinand": ['Leeds United', 'Manchester United'], "Gerard Pique": ['Barcelona', 'Manchester United']}
#Get a list of player in the dictionary
players = list(games_dict.keys())
#Get a list of clubs in the dictionary using a list comprehension
clubs =  [club for clublist in games_dict.values() for club in clublist]
#Select random player
player = random.choice(players)
#Get a random club (set is used to only select from unique clubs)
club = random.choice(list(set(clubs)))
#Ask for answer
inputanswer = input(f"Did {player} play for {club}? Y/N")

#Check if answer is correct
if inputanswer.lower() == "y" and club in games_dict[player] or inputanswer.lower() == "n" and club not in games_dict[player]:
    print("correct!")
else:
    print("incorrect!")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM