how do I break infinite while loop with user input

2019-08-27 16:24发布

I'm very new to Python.

I made a math program that keeps generating a new math problem every time you get the last one right. however I don't know how to exit/break the while True loop and switch from addition to subtraction.

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

how do I specify a user input (e.g. space bar) to brake the while True: loop. I've looked at other answers posted to similar questions but when I try them they all stop my code from generating more than a set number of problems.

Is there a way to do this. or do I need to find a way to run this math game without a while True loop?

1条回答
爷、活的狠高调
2楼-- · 2019-08-27 17:13

It is good to use while loop when the number of looping cannot be determined, Well done for your approach using while loop.

If you want to exit the while loop, you can try to define a variable(conditions variable) outside the while loop, and set the variable become your while conditions. And to exit the loop, just change the value of the conditions variable.

Code examples :

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")
查看更多
登录 后发表回答