![](/img/trans.png)
[英]Can't figure out how to make a python coin toss program loop back to a certain point once the whole process of the toss has been completed
[英]How to make a python program loop back to a certain line
我有以下代碼,如果發生某種偶然性,我希望它跳回到一行:
number_of_10ct = int(input("How many 10 Cent coins would you like to insert? "))
while number_of_10ct < 0:
number_of_10ct = int(input("Please enter a positive number."))
number_of_20ct = int(input("How many 20 Cent coins would you like to insert? "))
while number_of_20ct < 0:
number_of_20ct = int(input("Please enter a positive number."))
number_of_50ct = int(input("How many 50 Cent coins would you like to insert? "))
while number_of_50ct < 0:
number_of_50ct = int(input("Please enter a positive number."))
number_of_100ct = int(input("How many 1 Rand coins would you like to insert? "))
while number_of_100ct < 0:
number_of_100ct = int(input("Please enter a positive number."))
number_of_200ct = int(input("How many 2 Rand coins would you like to insert? "))
while number_of_200ct < 0:
number_of_200ct = int(input("Please enter a positive number."))
number_of_500ct = int(input("How many 5 Rand coins would you like to insert? "))
while number_of_500ct < 0:
number_of_500ct = int(input("Please enter a positive number."))
# Creating a variable to store the total amount of
# money inserted into the vending machine.
change = round(((number_of_10ct * 0.10) +
(number_of_20ct * 0.20) +
(number_of_50ct * 0.50) +
(number_of_100ct * 1.00) +
(number_of_200ct * 2.00) +
(number_of_500ct * 5.00)),
2)
# Informing the user how much they have entered in total.
print("\n")
print ('You have a credit of' ,change, 'ZAR')
while change > 0:
customer_choice = input(("What would you like to buy?"
"Type N when you are finished \n"))
if (customer_choice == "Kleiner Brauner" or
customer_choice == "kleiner brauner" and
change >= product_1_price):
print ("You have chosen a", product_1,
"these cost", product_1_price, "each,")
change = round((change - product_1_price),2)
print ("You have this much money remaining: R", change)
elif (customer_choice == "Kakao" or
customer_choice == "kakao" and
change >= product_2_price):
print ("You have chosen a", product_2,
"these cost", product_2_price, "each,")
change = round((change - product_2_price),2)
print ("You have this much money remaining: R", change)
elif customer_choice == "N" or customer_choice == "n":
break
elif change <= 0:
print ("You have run out of money.")
break
else:
print ("There has been an error or you may not have enough credit.")
我想發生的是:如果change = round((change - product_1_price),2)
最終為負,那么我希望程序返回頂部( number_of_10ct = int(input("How many 10 Cent coins would you like to insert? "))
),並允許用戶輸入更多的“金錢”。
我怎樣才能做到這一點?
您可以做的是將所有代碼放入一個額外的while
循環中,該循環將一直運行,直到change
變為肯定為止(因此,您需要在循環之前將change
初始化為負值)。
change = -1
while change < 0:
# All the rest of your code (re-indented to be under this while loop)
這樣,在將更改設置為良好值之后,您將進入while change > 0
循環。 一旦change
變為負數,您就可以退出循環並返回頂部,用戶可以在此處添加硬幣。
然后,如果客戶決定在使用所有零錢之前不買任何東西,那么change
仍然是正數,並且while change < 0
循環while change < 0
,您將擺脫困境。
使用while
循環和continue
語句:
while True:
dostuff()
if goback():
continue # back to start of loop
else:
break # code after the loop
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.