為班級做作業,我需要用戶輸入 1 到 500 之間的值,如果用戶輸入的錯誤值超過 3 次,則程式應該關閉。如果用戶需要重試,我也會遇到使程式回圈的問題
這是我到目前為止所擁有的。
flour = 2.75
baking_soda = 1
baking_powder = 0.5
butter = 1
sugar = 1.5
egg = 1
vanilla = 1
makes = 48
cookies = float(input('Enter number of cookies: '))
count = 0
flour = (cookies * flour) / makes
baking_soda = (cookies * baking_soda) / makes
baking_powder = (cookies * baking_powder) / makes
butter = (cookies * butter) / makes
sugar = (cookies * sugar) / makes
egg = (cookies * egg) / makes
vanilla = (cookies * vanilla) / makes
while (cookies >=1 and cookies <=500):
print("You need " str(round(flour,2))
" cups of flour, " str(round(baking_soda,2))
" teaspoons of baking soda, " str(round(baking_powder,2))
" teaspoons of baking powder, " str(round(butter,2))
" cups of butter, " str(round(sugar,2))
" cups of sugar, " str(round(egg,2))
" eggs and " str(round(vanilla,2))
" teaspoons of vanilla.")
break
count=0
while (cookies <1 or cookies >500):
count =1
cookies = float(input('Enter valid number: '))
if (count>=3):
print("I will now close.")
exit()
uj5u.com熱心網友回復:
這是現在的樣子,我認為這就是答案,因為它不會再給我帶來任何問題。
flour = 2.75
baking_soda = 1
baking_powder = 0.5
butter = 1
sugar = 1.5
egg = 1
vanilla = 1
makes = 48
cookies = int(input('Enter number of cookies from 1 to 500: '))
count=0
while (cookies <1 or cookies >500):
count =1
cookies = int(input('Enter valid number: '))
if (count>=3):
print("I will now close.")
exit()
while (cookies >=1 and cookies <=500):
flour = (cookies * flour) / makes
baking_soda = (cookies * baking_soda) / makes
baking_powder = (cookies * baking_powder) / makes
butter = (cookies * butter) / makes
sugar = (cookies * sugar) / makes
egg = (cookies * egg) / makes
vanilla = (cookies * vanilla) / makes
print("You need " str(round(flour,2))
" cups of flour, " str(round(baking_soda,2))
" teaspoons of baking soda, " str(round(baking_powder,2))
" teaspoons of baking powder, " str(round(butter,2))
" cups of butter, " str(round(sugar,2))
" cups of sugar, " str(round(egg,2))
" eggs and " str(round(vanilla,2))
" teaspoons of vanilla.")
break
uj5u.com熱心網友回復:
您有正確的想法,但您需要根據回圈的流程進行思考。要求用戶輸入總是很棘手,您總是希望在進行任何計算或作業之前嘗試驗證輸入。
有幾件事你似乎已經明白,你至少需要問一次。您可以在如下所示的單個回圈中完成所有這些操作。
我基本上是在創建一個無限回圈,只有在你出錯 5 次時才會停止。請注意,除非用戶至少一次做對,否則回圈將不會進行計算。請注意,在現實世界中,您將為用戶提供一個隨意退出的機會。嘗試以某種方式實作它,以便如果用戶輸入字母q,則程式結束。
請注意,我留下了一些小錯誤供您修復。如果你能解決它,你就會得到回圈的句柄。嘗試輸入 2 個 cookie,然后輸入 48 個 cookie。看看這個數字怎么沒有意義,有幾種方法可以解決這個問題,但關鍵是要理解為什么會出錯。
flour = 2.75
baking_soda = 1
baking_powder = 0.5
butter = 1
sugar = 1.5
egg = 1
vanilla = 1
makes = 48
count = 0
while (True):
cookies = float(input('Enter number of cookies: '))
if (cookies < 1 or cookies > 500):
print('Invalid response, the number must be between 1 and 500')
count = 1
if count > 4:
print("I will now close.")
break
continue
flour = (cookies * flour) / makes
baking_soda = (cookies * baking_soda) / makes
baking_powder = (cookies * baking_powder) / makes
butter = (cookies * butter) / makes
sugar = (cookies * sugar) / makes
egg = (cookies * egg) / makes
vanilla = (cookies * vanilla) / makes
print("You need " str(round(flour,2))
" cups of flour, " str(round(baking_soda,2))
" teaspoons of baking soda, " str(round(baking_powder,2))
" teaspoons of baking powder, " str(round(butter,2))
" cups of butter, " str(round(sugar,2))
" cups of sugar, " str(round(egg,2))
" eggs and " str(round(vanilla,2))
" teaspoons of vanilla.")
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/343066.html
上一篇:發布Blazor應用程式后,我收到錯誤“物件名稱無效”
下一篇:在PHP中使用嵌套回圈的質數
