[英]How do I continuously re-run a program in Python?
我创建了一个程序,该程序计算应用“折扣百分比”后可以节省多少钱。 用户能够输入产品的价格以及折价百分比。 程序完成计算后,如何连续循环程序,以便用户可以继续使用它,而不必每次在控制台中单击“运行”。
def percent_off_price():
price = amount * percent
amount = float(input('Enter the price of your product: '))
percent = float(input('Enter the percentage off: '))
price = amount * percent
print('You will save $',price,'off! Your total is: $',amount - price,)
将整个代码段放入一个while循环中,该循环始终运行,但这是您想要的最糟糕的事情:
while True:
amount = float(input('Enter the price of your product: '))
percent = float(input('Enter the percentage off: '))
price = amount * percent
print('You will save $',price,'off! Your total is: $',amount - price,)
您还可以检查支票以控制金额运行它。 因此,只有在输入的金额为正值时,它才会运行:
flag= True
while flag:
amount = float(input('Enter the price of your product: '))
if amount <0:
flag=False
percent = float(input('Enter the percentage off: '))
price = amount * percent
print('You will save $',price,'off! Your total is: $',amount - price,)
最后,如果要以固定编号运行它。 有时(假设10次),您可以进行for循环:
for i in range(10):
amount = float(input('Enter the price of your product: '))
percent = float(input('Enter the percentage off: '))
price = amount * percent
print('You will save $',price,'off! Your total is: $',amount - price,)
你可以做:
while True:
percent_off_price()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.