如何在 Python 中的某个点重新启动程序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19886171/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to restart a program at a certain point in Python
提问by user2975375
So I made a very primitive and probably inefficient calculator today (first time using Python), and I want to be able to continue doing more problems, how would I do so? Here is my "calculator" app..
所以我今天做了一个非常原始而且可能效率很低的计算器(第一次使用Python),我希望能够继续做更多的问题,我该怎么做?这是我的“计算器”应用程序..
import time
print ("Welcome. This is a calculator that uses the function: A (operator) B.")
time.sleep(3.5)
print ("Available operators include: Addition, Subtraction, Multiplication, Division, Exponent, and Remainder division.")
time.sleep(3.5)
a = float(input("Type in a value of A. "))
b = float(input("Type in a value of B. "))
operb = input("Would you like to: Add - Subtract - Multiply - Divide - Exponent - or Remainder? ")
opera = operb.lower()
if (opera) == "add":
print ((a) + (b))
elif (opera) == "subtract":
print ((a) - (b))
elif (opera) == "multiply":
print ((a) * (b))
elif (opera) == "divide":
print ((a) / (b))
elif (opera) == "exponent":
print ((a) ** (b))
elif (opera) == "remainder":
print ((a) % (b))
else:
print ("Invalid operation.")
cont = input("Would you like to do another problem?")
cont = cont.lower()
if (cont) == "yes":
??
else:
quit
I want it to restart at the "Type in a value of A." part, but I'm not sure how to do that.
我希望它在“键入 A 值”时重新启动。部分,但我不知道如何做到这一点。
回答by wvdz
The best way of doing this is probably with a while loop.
最好的方法可能是使用 while 循环。
while True:
## your code
if cont != "yes":
break
## quit
回答by tijko
You most likely going to want to use a while
loop, something like:
您很可能想要使用while
循环,例如:
import time
print ("Welcome. This is a calculator that uses the function: A (operator) B.")
time.sleep(3.5)
print ("Available operators include: Addition, Subtraction, Multiplication, Division, Exponent, and Remainder division.")
time.sleep(3.5)
while True:
a = float(input("Type in a value of A. "))
if a == 'q': # set up a condition to end the program
return
回答by Ramchandra Apte
Using a while loop, which keeps executing the block as long as the condition, cont == "yes"
, is true, i.e. it stops when the condition becomes false. After the while loop stops, the code after it is executed, in this case print("Bye, thanks for using the calculator.")
.
使用while 循环,只要条件cont == "yes"
为真,它就会继续执行块,即当条件变为假时它停止。while 循环停止后,将执行其后的代码,在本例中为print("Bye, thanks for using the calculator.")
.
PS: The brackets around a
and b
in print ((a) + (b))
are unnecessary. Similarly, the brackets around opera
and cont
are also unnecessary. Also, the space after print
makes it a little hard to see which function the arguments are part of. I'd suggest you remove the space. Otherwise for a beginner-level programmer the code is good. Once you become more experienced with Python, you might want to use a dictionary mapping the names of the operator into the functions in the operator
module.
PS:周围的括号a
,并b
在print ((a) + (b))
有必要的。同样,围绕支架opera
和cont
也不必要。此外,后面的空格print
使得很难看出参数属于哪个函数。我建议你删除空格。否则对于初学者级别的程序员来说,代码很好。一旦您对 Python 有了更多的经验,您可能希望使用字典将运算符的名称映射到operator
模块中的函数中。
import time
print ("Welcome. This is a calculator that uses the function: A (operator) B.")
time.sleep(3.5)
print ("Available operators include: Addition, Subtraction, Multiplication, Division, Exponent, and Remainder division.")
time.sleep(3.5)
cont = "yes" # So that the first time the while loop block will run
while cont == "yes":
a = float(input("Type in a value of A. "))
b = float(input("Type in a value of B. "))
operb = input("Would you like to: Add - Subtract - Multiply - Divide - Exponent - or Remainder? ")
opera = operb.lower()
if (opera) == "add":
print ((a) + (b))
elif (opera) == "subtract":
print ((a) - (b))
elif (opera) == "multiply":
print ((a) * (b))
elif (opera) == "divide":
print ((a) / (b))
elif (opera) == "exponent":
print ((a) ** (b))
elif (opera) == "remainder":
print ((a) % (b))
else:
print ("Invalid operation.")
cont = input("Would you like to do another problem?")
cont = cont.lower()
print("Bye, thanks for using the calculator.")