如何在Python的特定位置重新启动程序 [英] How to restart a program at a certain point in Python

查看:90
本文介绍了如何在Python的特定位置重新启动程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我今天做了一个非常原始且可能效率不高的计算器(第一次使用Python),我希望能够继续解决更多问题,我该怎么做?这是我的计算器应用。

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..

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。

I want it to restart at the "Type in a value of A." part, but I'm not sure how to do that.

推荐答案

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

The best way of doing this is probably with a while loop.

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

这篇关于如何在Python的特定位置重新启动程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆