我将如何修改它以将其放入函数中?

2024-07-08 17:18:34 发布

您现在位置:Python中文网/ 问答频道 /正文

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

这是一组三个问题,其中有三个答案我没有发布。我想知道如何将其放入名为main_questions()的函数中: 我是一个初学者,我看过多篇文章,但似乎不能理解他们。请帮忙


Tags: maingreenblueredwhatarecolorquestions
1条回答
网友
1楼 · 发布于 2024-07-08 17:18:34

我已经为您实施了一个可能的解决方案。为了更好地理解,我添加了几个命令。您可以尝试以下实现:

代码:

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

编辑:

如果要获取选择的颜色,可以定义包含键值对的dict。您可以对其使用以下功能(其他部分如下所示)

代码:

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

相关问题 更多 >

    热门问题