从列表中选择随机函数
Choosing random function from a list
我已经为“谁想成为百万富翁”类型的游戏定义了几个变量,其中包含问题、答案和类别(奖金金额)。然后我有这个功能,它根据这些问题、答案和诸如此类的东西运行。我尝试通过随机播放、选择、选择来使用 python 的随机函数,但没有成功。
您放入列表的不是函数,而是已经调用该函数的结果 -- 构建列表本身的行为在您有机会之前调用了 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
首先: 我建议您创建并保存所有问题 dictionary.
其次: 在 rnd.choice =
中,您尝试通过编写 =
来覆盖函数,该函数用于为等式之前的事物赋值标记。尝试查找 here.
最后:函数questionnaire()
没有return一个值,所以你不想像rnd.choice=([questionnaire(question1,answers1,correct1,amount1,cat1), ...
那样使用它
我已经为“谁想成为百万富翁”类型的游戏定义了几个变量,其中包含问题、答案和类别(奖金金额)。然后我有这个功能,它根据这些问题、答案和诸如此类的东西运行。我尝试通过随机播放、选择、选择来使用 python 的随机函数,但没有成功。
您放入列表的不是函数,而是已经调用该函数的结果 -- 构建列表本身的行为在您有机会之前调用了 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
首先: 我建议您创建并保存所有问题 dictionary.
其次: 在 rnd.choice =
中,您尝试通过编写 =
来覆盖函数,该函数用于为等式之前的事物赋值标记。尝试查找 here.
最后:函数questionnaire()
没有return一个值,所以你不想像rnd.choice=([questionnaire(question1,answers1,correct1,amount1,cat1), ...
那样使用它