Doing an assignment for class and I need the user to enter a value between 1 and 500 and if the user enters an incorrect value more than three times then the program should close. I'm also having issues making the program loop if the user needs to try again
Here's what I have so far.
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()
CodePudding user response:
Here's what it looks like now, I think this is the answer as it's not giving me any more problems.
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
CodePudding user response:
You have the right ideas, but you need to think in terms of the flow of your loop. Asking for input for the user is always tricky, you always want to to try to verify the input before doing any computation or work.
There are a few things you seem to have understood, that you need to ask at least once. You can do all of this in the single loop like below.
I'm basically creating an infinite loop that will only stop when you get it wrong 5 times. Notice how unless the user gets it right at least one time, the loop won't proceed to the computation. Note that the real world, you would provide an opportunity to the user to quit at their will. Try to implement it in a way so that if the user enters the letter q
, the program ends.
Be aware that I have left small bug for you to fix. If you can resolve it, you'll get the handle of loops. Try to enter 2 cookies and then 48 cookies. See how the number don't make sense, there are a few ways to resolve this, but the key is to understand why it gets wrong.
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.")