如何在 Python 中的某个点重新启动程序

use*_*375 2 python calculator python-3.x

所以我今天做了一个非常原始而且可能效率低下的计算器(第一次使用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
Run Code Online (Sandbox Code Playgroud)

我希望它在“键入 A 值”时重新启动。部分,但我不知道如何做到这一点。

wvd*_*vdz 5

最好的方法可能是使用 while 循环。

while True:
    ## your code
    if cont != "yes":
        break
## quit
Run Code Online (Sandbox Code Playgroud)

  • ...在 Python 中正确地写为 `while True:` 而不是 `while (true):`。 (2认同)