如何在用户输入的情况下打破无限循环

时间:2018-05-07 09:45:08

标签: python-3.x while-loop infinite-loop

我对Python很陌生。

我做了一个数学程序,每当你得到最后一个时,它就会产生一个新的数学问题。但是我不知道如何退出/打破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

如何指定用户输入(例如空格键)来制动while:循环。我已经查看了发布到类似问题的其他答案,但是当我尝试它们时,它们都会阻止我的代码产生超过一定数量的问题。

有没有办法做到这一点。或者我是否需要找到一种方法来运行这个数学游戏而没有一段时间的True循环?

1 个答案:

答案 0 :(得分:0)

当无法确定循环次数时,最好使用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")
相关问题