mercredi 21 août 2019

How to see if player answers the value that corresponds to the key of that value

Write a simple quiz game that has a list of ten questions and a list of answers to those questions. The game should give the player four randomly selected questions to answer. It should ask the questions one-by-one, and tell the player whether they got the question right or wrong. At the end it should print out how many out of four they got right.

I've created one function that chooses one random key from my dictionary. My dictionary consists of 10 keys ('What is the capital of 'a' country) and each key has one answer (the capital of that country 'a'). Imagine i have (for now the 3 following functions).

import random

questions = {'What is the capital of Portugal':'Lisbon','What is the capital of France':'Paris','What is the capital of Germany':'Berlin','What is the capital of Netherlands':'Amsterdam','What is the capital of Belgium':'Brussels','What is the capital of Nepal':'Kathmandu','What is the capital of Spain':'Madrid','What is the capital of England':'London','What is the capital of Ireland':'Dublin','What is the capital of United_States':'Washington'} #dictionary with all questions and answers

#function that returns random question from dict
def questions_sorting():

  return random.sample(list(questions.keys()),1)

print(questions_sorting())


#function that asks player for capital input
def player_answer():

  return input('Digit your final answer - \n')

def countries_game():

  right_ans = 0 #counts the number of right answers 
  wrong_ans = 0 #counts the number of wrong answers
  num_questions = 0 #counts the number of questions displayed so far

  while num_questions < 4: #there will be four questions to be displayed
    cpu_question = questions_sorting()
    player_choice = player_answer()
    num_questions += 1

How can i see if a player enters the correct value for the the asked key?. Also in my question sort function i am sorting one question . How can i ask another question without the possibility of asking the same question?




Aucun commentaire:

Enregistrer un commentaire