询问用户是否希望在Python中使用while循环再次播放

2024-06-16 12:49:21 发布

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

我想问用户是否想使用while循环再次播放。这是我的密码。我怎么做?如果我将另一个while循环堆叠在我的另一个while循环之上,我将收到一个错误。我不确定我是否做对了

QM = ["The average of first 50 natural numbers is _____\na. 25.30\nb. 25.5\nc. 25.00\nd. 12.25\nAnswer: ",
      "A clock strikes once at 1 o’clock, twice at 2 o’clock, thrice at 3 o’clock and so on. How many times will it strike in 24 hours?\na. 78\nb. 136\nc. 156\nd. 196\nAnswer: ",
      "106 × 106 – 94 × 94 = ?\na. 2004\nb. 2400\nc. 1904\nd. 1906\nAnswer: ",
      "Which of the following numbers gives 240 when added to its own square?\na. 15\nb. 16\nc. 18\nd. 20\nAnswer: ",
      "If David’s age is 27 years old in 2011. What was his age in 2003?\na. 17 years\nb. 37 years\nc. 20 years\nd. 19 years\nAnswer: ",
      "What is 7% equal to?\na. 0.007\nb. 0.07\nc. 0.7\nd. 7\nAnswer: ",
      "I am a number. I have 7 in the ones place. I am less than 80 but greater than 70. What is my number?\na. 71\nb. 73\nc. 75\nd. 77\nAnswer: ",
      "How many years are there in a decade?\na. 5\nb. 10\nc. 15\nd. 20\nAnswer: ",
      "What is the square of 15?\na. 15\nb. 30\nc. 252\nd. 225\nAnswer: ",
      "In a century how many months are there?\na. 12\nb. 120\nc. 1200\nd. 12,000\nAnswer: ",
      "If a number has an even number or zero at its unit place; the number is always divisible by ____\na. 2\nb. 5\nc. 3\nd. 7\nAnswer: ",
      "An acute angle is ____\na. 90 degree.\nb. less than 90 degree.\nc. more than 90 degree.\nd. None of these.\nAnswer: ",
      "What is the opposite of 6?\na. 6\nb. 5\nc. 4\nd. -6\nAnswer: ",
      "Absolute value of -20 or |-20| is ________\na. 0\nb. -20\nc. |-20|\nd. 20\nAnswer: ",
      "Which of these following set of numbers are factors of 24?\na. 2, 3, 4, 6, 8\nb. 1, 5, 12, 18\nc. 4, 7, 24\nd. 3, 9, 12\nAnswer: "
      ]

questionsEM = [
    Question(QM[0], "b"),
    Question(QM[1], "c"),
    Question(QM[2], "b"),
    Question(QM[3], "a"),
    Question(QM[4], "d"),
    Question(QM[5], "b"),
    Question(QM[6], "d"),
    Question(QM[7], "b"),
    Question(QM[8], "d"),
    Question(QM[9], "c"),
    Question(QM[10], "a"),
    Question(QM[11], "b"),
    Question(QM[12], "d"),
    Question(QM[13], "d"),
    Question(QM[14], "a"),
]

print("=====QUIZ APPLICATION=====")
print ("=====INSTRUCTION====")
print ("""Every subject, they have their own difficulties. Every difficulties consists of 10 questions.
The difficulties are: Easy, Medium, and Hard.""")
print ("THE SUBJECTS FOR THE QUIZ ARE:")

如果用户想再次播放,我想在这里重新开始该程序

    print("1. Math")
    print ("2. English")
    print ("3. Science")
    while True:
        subj = input("Choose a subject by typing the number: ")
        subj = int(subj)
        if subj > 3:
            print("The number does not define the subject.")
            continue
        else:
            if subj == 1:
                print("===================================")
                print("You have chosen the subject Math")
                print("First difficulty will be EASY.")
                print("Let's proceed to the quiz! GOOD LUCK!\n")
                def run_EM(questionsEM):
                    score = 0
                    random.shuffle(questionsEM)
                    for question in questionsEM[:10]:
                        answer = input(question.prompt).lower()
                        if answer == question.answer:
                            score += 1
                            print("Correct")
                        else:
                            print("Wrong")
                            print("You got " + str(score) + "/" + str(10) + " correct")
                            print("Game Over! You did not passed the easy level")

询问用户是否想再次播放

                         #again = str(input("Do you want to play again? [Y] / [N]: "))
                         #if again == "N" or "n":
                               break
                        if score == 10:
                            print("You got " + str(score) + "/" + str(10) + " correct")
                            print("==============================")
                            print("YOU MOVED ON FROM EASY LEVEL. LET'S GO FOR THE MEDIUM LEVEL. STARTING NOW.....")
                            print("==============================")
                            import QuestionMM
                run_EM(questionsEM)
                break

Tags: oftheinnumberiswhatquestionprint
2条回答

你的错误是:

if again == "N" or "n":

您的if条件有两个条件:再次==“N”或“N”。再次==“N”是一个有效条件,如果再次等于“N”,则该条件可能为True,如果再次等于任何其他条件,则该条件可能为False。但是,第二个条件“n”将始终为True,因为对非空字符串(“”)的条件将变为True条件

import QuestionMM
def run_EM(questionsEM):
    score = 0
    random.shuffle(questionsEM)
    for question in questionsEM[:10]:
        answer = input(question.prompt).lower()
        ...
        again_request = str(input("Do you want to play again? [Y] / [N]: "))
        if again_request == "N" or again_request == "n":
            return False
        ...
    return True
QM = [...]
questionsEM = [...]
...
again = True
while again:
    subj = input("Choose a subject by typing the number: ")
    subj = int(subj)
    if subj > 3:
        print("The number does not define the subject.")
        continue
    else:
        if subj == 1:
            ...
            again = run_EM(questionsEM)

更新

在再次查看代码之后,您似乎遇到了另一个问题,即在函数中再次设置,因此需要返回一个值,以便while循环停止。我已经相应地更新了代码

感谢@DarrylG的反馈

当用户不想再次执行游戏时,在while循环中使用break是很好的。另一种方法是:

playAgain = True
while playAgain:
    # Game body here
    ans = str(input("Do you want to play again? [Y] / [N]: "))
    if (ans == "N") or (ans == "n"):
        playAgain = False

此外,尽可能不要试图在if条件内定义函数,只需调用它并在调用函数外定义它即可

相关问题 更多 >