[英]I want to make a nested loop that makes user enter a number that does not make the input variable have a negative output. How with changing variable?
這是 Visual Studio Code 中的 Python。 我正在為學校制作一個程序,它本質上是一個機器人餐廳女服務員。 我必須按照作業大綱所說的方式去做,這就是為什么它可能有點奇怪。 然后我必須個性化它並使其具有創意。 它詢問成人餐和兒童餐的價格是多少。 然后它詢問有多少兒童和成人。 然后它詢問銷售稅率是多少。 作業只需要我能夠計算所有這些並得到小計和總計然后找零。 我已經做到了,而且我正在把它做得很漂亮。 我做了一個循環,詢問他們是否有一張幸運抽獎券,可以生成一個介於 $1.00-$5.00 之間的隨機 m.netary 值。 然后如果他們說是,它將把他們帶到循環的第一部分,給他們提供優惠券的總額,然后要求付款。 如果他們說不,那么它只是要求他們付款。
我想加倍努力 go 並在循環中放置一個循環,我認為這稱為嵌套循環(這是我 Python 的第一周。)我希望它能夠識別他們支付的金額是否少於總額,然后要求用戶再試一次,直到他們輸入的金額超過總數。 幾乎我只是想讓它知道付款還不夠。
我的問題是我從未使用過嵌套循環,這是我什至第一次使用循環周期。 我也不知道如何基於不是常數的變量進行循環,因為它基於提示時用戶輸入的內容。 如果我能把參數放在一個固定的范圍內,我就能做到,但范圍總是會根據它們一開始輸入的內容而有所不同。
如何使嵌套循環識別一個永遠不相同的變量? 我如何讓它認識到付款不足以支付總額?
這是我的循環。 我想在循環中放置兩個嵌套循環。 一個用於 if,一個用於 elif,以便它適用於兩個“選項”。 這是我的片段:
while True:
coup = input("Do you have a lucky draw coupon? 1 for YES 2 for NO: ")
if coup =="1":
#calling the random function and also rounding it so that it comes out as two decimal places
coupon = round(random.uniform(1.00,5.00),2)
print()
print("---------------------")
#tells the user how much they save and the :.2f is making sure it has two decimal places
print(f"You will save ${coupon:.2f}!")
print("---------------------")
print()
newtotal = total - coupon
print(f"Your new total is: ${newtotal:.2f}")
print()
payment = float(input("Please enter your payment amount: "))
change2 = payment - total + coupon
change2 = round(change2, 2)
print(f"Change: ${change2:.2f}")
print()
print("---------------------------------------------------------------------")
print("Thank you party of", capitalized_string, "for choosing Erin's Cafe! Have a great day!")
print("---------------------------------------------------------------------")
print()
#break stops the loop from looping again
break
elif coup == "2":
print()
print("That's ok! Maybe next time!")
print()
payment = float(input("Please enter your payment amount: "))
change = payment - total
change = round(change, 2)
print(f"Change: ${change:.2f}")
print()
print("---------------------------------------------------------------------")
print("Thank you party of", capitalized_string, "for choosing Erin's Cafe! Have a great day!")
print("---------------------------------------------------------------------")
print()
break
else:
print()
print("Invalid response. Please type 1 for YES and 2 for NO: ")
有什么建議么? 我搜索了整個 Stackoverflow 和谷歌,但找不到適合我情況的任何內容。
您的方法有利於實現這一點。 在要求付款時循環並在允許循環中斷之前驗證它是否正確將完成這項工作。 對於您的第一個if
塊,它看起來是這樣的:
while True:
coup = input("Do you have a lucky draw coupon? 1 for YES 2 for NO: ")
if coup =="1":
#calling the random function and also rounding it so that it comes out as two decimal places
coupon = round(random.uniform(1.00,5.00),2)
print()
print("---------------------")
#tells the user how much they save and the :.2f is making sure it has two decimal places
print(f"You will save ${coupon:.2f}!")
print("---------------------")
print()
newtotal = total - coupon
print(f"Your new total is: ${newtotal:.2f}")
print()
#Ask for the payment inside of the new loop
while True:
payment = float(input("Please enter your payment amount: "))
if payment - total + coupon > 0:
print("That's not enough money to pay your bill. Try again")
else:
break
change2 = payment - total + coupon
change2 = round(change2, 2)
print(f"Change: ${change2:.2f}")
print()
print("---------------------------------------------------------------------")
print("Thank you party of", capitalized_string, "for choosing Erin's Cafe! Have a great day!")
print("---------------------------------------------------------------------")
print()
#break stops the loop from looping again
break
elif coup == "2":
print()
print("That's ok! Maybe next time!")
print()
payment = float(input("Please enter your payment amount: "))
change = payment - total
change = round(change, 2)
print(f"Change: ${change:.2f}")
print()
print("---------------------------------------------------------------------")
print("Thank you party of", capitalized_string, "for choosing Erin's Cafe! Have a great day!")
print("---------------------------------------------------------------------")
print()
break
else:
print()
print("Invalid response. Please type 1 for YES and 2 for NO: ")
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.