如何使用用户输入打破无限 while 循环

how do I break infinite while loop with user input

我是 Python 的新手。

我制作了一个数学程序,每次你答对最后一个问题时,它都会不断生成一个新的数学问题。但是我不知道如何 exit/break while True 循环并从加法切换到减法。

import random

while True:

    question = input("press 1 for addition, press 2 for subtraction.")
    if question == "1":
        print("Try your skill at these problems.")
        number1 = random.randint(0, 9)
        number2 = random.randint(0, 9)

        while True:
            print(number1, "+", number2, "=")
            number3 = number1+number2
            answer = int(input())
            if answer == number3:
                print("Great Job, Try this one.")
                number1 = random.randint(0, 9)
                number2 = random.randint(0, 9)
            else:
                print("Have another go")
            #I want to push space to break this while loop
            #and be able to switch to subtraction problems    
    if question == "2":
        print("Try your skill at these problems.")
        number1 = random.randint(0, 9)
        number2 = random.randint(0, 9)

        while True:
            print(number1, "-", number2, "=")
            number3 = number1-number2
            answer = int(input())
            if answer == number3:
                print("Great Job, Try this one.")
                number1 = random.randint(0, 9)
                number2 = random.randint(0, 9)
            else:
                print("Have another go")
            #I want to push space to break this while loop
            #and be able to switch to addition problems

如何指定用户输入(例如 space 栏)来阻止 while True: 循环。我看过针对类似问题发布的其他答案,但是当我尝试它们时,它们都会阻止我的代码产生超过一定数量的问题。

有没有办法做到这一点。还是我需要找到一种方法来 运行 这个数学游戏而不需要 while True 循环?

在无法确定循环次数的情况下使用while循环是个好办法,你的while循环方法做得很好。

如果想退出while循环,可以尝试在while循环外定义一个变量(条件变量),将变量设置为你的while条件。要退出循环,只需更改条件变量的值即可。

代码示例:

import random

while True:

    conditions = True # my changes
    question = input("press 1 for addition, press 2 for subtraction.")
    if question == "1":
        print("Try your skill at these problems.")
        number1 = random.randint(0, 9)
        number2 = random.randint(0, 9)

        while conditions: # my changes
            print(number1, "+", number2, "=")
            number3 = number1+number2
            answer = int(input())
            if answer == number3:
                print("Great Job, Try this one.")
                number1 = random.randint(0, 9)
                number2 = random.randint(0, 9)
                checking = input("Another round? (Y/N)") # my changes
                if checking == "N": # my changes
                    conditions = False # my changes
            else:
                print("Have another go")
    if question == "2":
        print("Try your skill at these problems.")
        number1 = random.randint(0, 9)
        number2 = random.randint(0, 9)

        while conditions: # my changes
            print(number1, "-", number2, "=")
            number3 = number1-number2
            answer = int(input())
            if answer == number3:
                print("Great Job, Try this one.")
                number1 = random.randint(0, 9)
                number2 = random.randint(0, 9)
                checking = input("Another round? (Y/N)") # my changes
                if checking == "N": # my changes
                    conditions = False # my changes
            else:
                print("Have another go")