为班级做作业,我需要用户输入一个介于 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()