dimanche 31 octobre 2021

Choosing random function from a list

I've defined several variables with the questions, answers and category (Prize amount) for a "who wants to be a millionaire" kind of game. Then I have this function who runs based on those Questions, answers and whatnot. I've tried to use the random function of python through shuffle, choice, choices and haven't had success.

These are the question sort of format:

question1 = "QUESTION: What is the biggest currency in Europe?"
answers1 = ["A) Crown", "B) Peso", "C) Dolar", "D) Euro"]
correct1 = "D"
amount1 = 25
cat1= 1

question2 = "QUESTION: What is the biggest mountain in the world?"
answers2 = ["A) Everest", "B) Montblanc", "C) Popocatepepl", "D) K2"]
correct2 = "A"
amount2 = 25
cat2= 2

question3 = "QUESTION: What is the capital of Brasil?"
answers3 = ["A) Rio de Janeiro", "B) Brasilia", "C) Sao Paolo", "D) Recife"]
correct3 = "B"
amount3 = 25
cat3= 3

This is what I've tried: makign a list of those functions for the first category. It always prompts me the very first question of the list no matter what. Also, it also ignores the conditions set inside the main function who, in case you retire or have a wrong answer, the game is over.

rnd.choice=([questionnaire(question1,answers1,correct1,amount1,cat1), 
questionnaire(question2,answers2,correct2,amount2,cat2), 
questionnaire(question3,answers3,correct3,amount3,cat3),questionnaire(question4,answers4,correct4,amount4,cat4), questionnaire(question5,answers5,correct5,amount5,cat5)])

Here's the code for the function questionnaire:

def questionnaire (question,answers,correct,amount, cat):
 print (question) #Shows the question
 for answer in answers: #loop through answers, print answer.
  print(answer)
  usr_input_answer= input(" What's your answer? Please select between A, B, C, D or R for Retirement. ")
  if usr_input_answer.upper() == correct:
   moneymaker(amount)
  elif usr_input_answer.upper() == "R":
   retire()
  else:
   gameover()

Both retire and gameover functions will go back and set the status variable to 0 to prevent the game from running again. I tried running this random function inside a while loop, comparing this status variable and it ignores it.

TIA.




Aucun commentaire:

Enregistrer un commentaire