Python 我将如何修改它以将其放入函数中?
这是一组三个问题,其中有三个答案我没有发布。我正试图弄清楚如何将其放入名为main_questions的函数中:Python 我将如何修改它以将其放入函数中?,python,python-3.x,Python,Python 3.x,这是一组三个问题,其中有三个答案我没有发布。我正试图弄清楚如何将其放入名为main_questions的函数中: 我是一个初学者,我看过多篇文章,但似乎不能理解他们。请帮忙?我已经为您实施了一个可能的解决方案。为了更好地理解,我添加了几个命令。您可以尝试以下实现: 代码: 用法: 编辑: 如果要获取选择的颜色,可以定义包含键值对的dict。您可以使用以下功能,其他部分如下所示 代码: 输出: 函数用于完成和/或返回内容;你想让这个函数做什么和/或返回什么?你想把它放在函数中实现什么?@Scott
我是一个初学者,我看过多篇文章,但似乎不能理解他们。请帮忙?我已经为您实施了一个可能的解决方案。为了更好地理解,我添加了几个命令。您可以尝试以下实现: 代码: 用法: 编辑: 如果要获取选择的颜色,可以定义包含键值对的dict。您可以使用以下功能,其他部分如下所示 代码: 输出:
函数用于完成和/或返回内容;你想让这个函数做什么和/或返回什么?你想把它放在函数中实现什么?@ScottHunter我需要这个函数在屏幕上返回这些问题中的每一个。在屏幕上返回什么意思?你是说打印吗?@bat_s08可能会使用循环来显示问题。您还可以使用字典将答案映射到问题,并检查用户是否选择了正确答案。
question_prompts = [
"What color are Apples?\n(a) Red/Green\n(b) Purple\n(c) Orange\n\n",
"What color are Bananas?\n(a) Blue\n(b) Yellow\n(c) Green\n\n",
"What color are Strawberries?\n(a) Red\n(b) Blue\n(c) Green\n\n" ]
def main_questions():
question_prompts = [
"What color are Apples?\n(a) Red/Green\n(b) Purple\n(c) Orange\n\n",
"What color are Bananas?\n(a) Blue\n(b) Yellow\n(c) Green\n\n",
"What color are Strawberries?\n(a) Red\n(b) Blue\n(c) Green\n\n"]
def main_questions():
answer_list = [] # This list will contain the all answers
answer_choices = ["a", "b", "c"] # The possible answers
for single_question in question_prompts:
while True:
single_answer = input(single_question)
if single_answer in answer_choices: # Break the while loop if get a valid answer.
break
print("Your answer is not in: {}".format(answer_choices))
answer_list.append(single_answer) # Append the correct answer to the return list.
return answer_list # Return the all valid answers
answers = main_questions()
print("\nAnswers:\n")
for idx, answer in enumerate(answers):
print("{}. - {}".format(idx, answer))
>>> python3 test.py
What color are Apples?
(a) Red/Green
(b) Purple
(c) Orange
a
What color are Bananas?
(a) Blue
(b) Yellow
(c) Green
b
What color are Strawberries?
(a) Red
(b) Blue
(c) Green
g
Your answer is not in: ['a', 'b', 'c']
What color are Strawberries?
(a) Red
(b) Blue
(c) Green
b
Answers:
0. - a
1. - b
2. - b
def main_questions():
answer_list = [] # This list will contain the all answers
answer_choices = {1: {"a": "Red/Green", "b": "Purple", "c": "Orange"},
2: {"a": "Blue", "b": "Yellow", "c": "Green"},
3: {"a": "Red", "b": "Blue", "c": "Green"}}
answer_counter = 1
for single_question in question_prompts:
while True:
single_answer = input(single_question)
if single_answer in answer_choices[answer_counter]: # Break the while loop if get a valid answer.
break
print("Your answer is not in: {}".format(answer_choices[answer_counter]))
answer_list.append(answer_choices[answer_counter][single_answer]) # Append the correct answer to the return list.
answer_counter += 1
return answer_list # Return the all valid answers
>>> python3 test.py
What color are Apples?
(a) Red/Green
(b) Purple
(c) Orange
a
What color are Bananas?
(a) Blue
(b) Yellow
(c) Green
b
What color are Strawberries?
(a) Red
(b) Blue
(c) Green
g
Your answer is not in: {'a': 'Red', 'b': 'Blue', 'c': 'Green'}
What color are Strawberries?
(a) Red
(b) Blue
(c) Green
b
Answers:
0. - Red/Green
1. - Yellow
2. - Blue