[英]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.