how do I break infinite while loop with user input

ε祈祈猫儿з 提交于 2019-12-25 01:28:49

问题


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:


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")


来源:https://stackoverflow.com/questions/50211240/how-do-i-break-infinite-while-loop-with-user-input

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!