为class赋值,我需要用户输入1到500之间的值,如果用户输入不正确的值超过三次,那么程序应该关闭。我也有问题,使程序循环,如果用户需要再次尝试
这是我到目前为止写的。
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()
这是它现在的样子,我想这就是答案了,因为它不再给我任何问题。
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
您的想法是正确的,但是您需要考虑循环的流程。请求用户输入总是很棘手的,你总是想在做任何计算或工作之前尝试验证输入。
有几件事你似乎已经明白了,你需要至少问一次。您可以像下面这样在单个循环中完成所有这些操作。我基本上是在创建一个无限循环,只有当你出错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.")