繁体   English   中英

从列表中选择随机函数

[英]Choosing random function from a list

我已经为“谁想成为百万富翁”类型的游戏定义了几个带有问题、答案和类别(奖金金额)的变量。 然后我有这个功能,它根据这些问题、答案和诸如此类的东西运行。 我尝试通过shuffle、choice、choices来使用python的随机函数,但没有成功。

这些是问题的格式:

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

这就是我尝试过的:为第一类创建这些函数的列表。 无论如何,它总是提示我列表的第一个问题。 此外,它还忽略主函数中设置的条件,如果您退出或有错误的答案,游戏就结束了。

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)])

下面是函数问卷的代码:

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()

Retire 和 gameover 函数都将返回并将状态变量设置为 0 以防止游戏再次运行。 我尝试在 while 循环中运行这个随机函数,比较这个状态变量并忽略它。

TIA。

您放入列表的不是函数,而是已经调用该函数的结果——在您有机会从中挑选元素之前,构建列表本身的行为会调用questionnaire三遍。

将每个问题放入一个对象中,而不是拥有一组唯一的命名变量,这样可以更容易地将随机问题作为一个单元。 您可以为此使用dict 我通常使用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

首先:我建议您将所有问题创建并保存在字典中。

其次:rnd.choice =您尝试通过编写=来覆盖函数,该函数用于为等式标记之前的事物赋予价值。 试试看这里。

最后:函数questionnaire()不返回值,所以你不rnd.choice=([questionnaire(question1,answers1,correct1,amount1,cat1), ...

暂无
暂无

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

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