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

时间:2013-11-10 04:09:06

标签: python python-3.x calculator

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

我希望它在“键入A值”时重新启动。部分,但我不知道该怎么做。

3 个答案:

答案 0 :(得分:4)

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

while True:
    ## your code
    if cont != "yes":
        break
## quit

答案 1 :(得分:3)

使用while loop,只要条件cont == "yes"为真,它就会一直执行该块,即当条件变为假时它会停止。 while循环停止后,执行后的代码,在本例中为print("Bye, thanks for using the calculator.")

PS a中的bprint ((a) + (b))周围的括号是不必要的。同样,operacont周围的括号也是不必要的。此外,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.")

答案 2 :(得分:1)

您很可能想要使用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