问题描述
我是Python的新手.
I'm very new to Python.
我制作了一个数学程序,每次您获得最后一个正确答案时,都会不断产生新的数学问题.但是我不知道如何退出/中断while True循环并从加法转换为减法.
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
如何指定用户输入(例如空格键)来制动while True:循环.我看过发布给类似问题的其他答案,但是当我尝试它们时,它们都阻止了我的代码生成大量问题.
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.
有没有办法做到这一点.还是我需要找到一种在没有True循环的情况下运行此数学游戏的方法?
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?
推荐答案
当无法确定循环次数时,最好使用while循环,对于使用while循环的方法来说做得很好.
It is good to use while loop when the number of looping cannot be determined, Well done for your approach using while loop.
如果要退出while循环,可以尝试在while循环之外定义一个变量(条件变量),并将该变量设置为while条件.要退出循环,只需更改条件变量的值即可.
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.
代码示例:
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")
这篇关于我如何打破无限的while循环与用户输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!