简体   繁体   English

从列表中选择随机函数

[英]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.我尝试通过shuffle、choice、choices来使用python的随机函数,但没有成功。

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. Retire 和 gameover 函数都将返回并将状态变量设置为 0 以防止游戏再次运行。 I tried running this random function inside a while loop, comparing this status variable and it ignores it.我尝试在 while 循环中运行这个随机函数,比较这个状态变量并忽略它。

TIA. TIA。

What you're putting into the list isn't the function, it's the result of having already called the function -- the act of building the list itself calls questionnaire three times before you have a chance to pick an element out of it.您放入列表的不是函数,而是已经调用该函数的结果——在您有机会从中挑选元素之前,构建列表本身的行为会调用questionnaire三遍。

Putting each question into an object rather than having sets of unique named variables makes it easier to pick a random question as a unit.将每个问题放入一个对象中,而不是拥有一组唯一的命名变量,这样可以更容易地将随机问题作为一个单元。 You could use a dict for this;您可以为此使用dict I usually use NamedTuple .我通常使用NamedTuple

from random import choice
from typing import List, NamedTuple, Tuple


class Question(NamedTuple):
    question: str
    answers: List[str]
    correct: str
    amount: int
    cat: int


questions = [
    Question(
        "QUESTION: What is the biggest currency in Europe?",
        ["A) Crown", "B) Peso", "C) Dolar", "D) Euro"],
        "D", 25, 1
    ),
    Question(
        "QUESTION: What is the biggest mountain in the world?",
        ["A) Everest", "B) Montblanc", "C) Popocatepepl", "D) K2"],
        "A", 25, 2
    ),
    Question(
        "QUESTION: What is the capital of Brasil?",
        ["A) Rio de Janeiro", "B) Brasilia", "C) Sao Paolo", "D) Recife"],
        "B", 25, 3
    ),
]


def questionnaire(q: Question) -> Tuple[int, bool]:
    """Presents the user with the given question.
    Returns winnings and whether to continue the game."""
    print(q.question)
    for answer in q.answers:
        print(answer)
    usr_input_answer = input(
        " What's your answer? "
        "Please select between A, B, C, D or R for Retirement. "
    ).upper()
    if usr_input_answer == q.correct:
        return q.amount, True
    elif usr_input_answer == "R":
        print("Congratulations on retirement!")
    else:
        print("Game over!")
    return 0, False


money = 0
keep_playing = True
while keep_playing:
    winnings, keep_playing = questionnaire(choice(questions))
    money += winnings

Firstly: I suggest you to create and keep all the questions within a dictionary.首先:我建议您将所有问题创建并保存在字典中。

Secondly: In rnd.choice = you try to overwrite the function by writing = which is used to give value to the thing that comes before the equation mark.其次:rnd.choice =您尝试通过编写=来覆盖函数,该函数用于为等式标记之前的事物赋予价值。 Try looking up here.试试看这里。

Lastly: The function questionnaire() doesn't return a value, so you don't wanna use it like rnd.choice=([questionnaire(question1,answers1,correct1,amount1,cat1), ...最后:函数questionnaire()不返回值,所以你不rnd.choice=([questionnaire(question1,answers1,correct1,amount1,cat1), ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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