Working on Python Homework, Having Trouble With Loop -
Working on Python Homework, Having Trouble With Loop -
i'm in beginner's python course of study @ university. part of homework create programme resembles famous children's song 99 bottles of pop. task is:
the user inputs item. the programme prints out statement such: "x bottles of pop on wall, x bottles of pop. take 1 down, pass around, (x-1) bottles of pop on wall" until reaches 0. 1 time reaches 0, programme must stop , there no bottles of pop on wall. the limit 99. if user inputs above 99, display error , forcefulness counter begin @ 99.makes sense right? so, here code:
#bottles of pop on wall #user inputs number start #program returns singing popular song downwards 0 # print("bottles of pop on wall program") userbottle = int(input("how many bottles? ")) bottlecount = userbottle while bottlecount > 1: newbottle = userbottle - 1 bottlecount -= 1 if bottlecount > 99: print("alert: no more 99 bottles allowed, reverting 99 bottles") userbottle = 99 bottlecount = 99 newbottle = 99 print(userbottle , "bottles of pop on wall, ", userbottle , "bottles of pop" , "\ntake 1 down, pass around, " , newbottle , "bottles of pop on wall") userbottle -= 1 if bottlecount == 1: print(userbottle , "bottle of pop on wall, ", userbottle , "bottle of pop" , "\ntake 1 down, pass around, " , "no bottles of pop on wall") input("\nthank playing! press come in exit.") so, if user inputs number below 100, programme works perfectly. however, if user inputs above 99, that's run problems.
what happen loop run downwards 1, not end. repeat 1 lastly time, , return:
1 bottles of pop on wall, 1 bottles of pop take 1 down, pass around, 0 bottles of pop on wall 0 bottle of pop on wall, 0 bottle of pop take 1 down, pass around, no bottles of pop on wall give thanks playing! press come in exit. obviously incorrect. wrong loop can create sure doesn't happen when user enters number bigger 99?
thank much , appreciate help!
it looks have aliasing problem.
outside loop inquire user come in number, come in 101, , set bottlecount = userbottle. within loop reset value of userbottle 99. observe following:
in [6]: userbottle = 101 in [7]: bottlecount = userbottle in [8]: userbottle = 99 in [9]: userbottle out[9]: 99 in [10]: bottlecount out[10]: 101 the next program. may have changed userbottle value, have not changed bottlecount value.
the thing write function userbottle value in controlled way, , function returns itslef, until value correct. illustration might be:
def get_user_bottles(input_message, error_message1, error_message2): #this makes sure user inputs number try: userbottle = int(raw_input(input_message)) except valueerror: print error_message1 homecoming get_user_bottles(input_message, error_message1, error_message2) #this makes sure user inputs number between 1 , 99 if userbottle not in range(1, 100): print error_message2 homecoming get_user_bottles(input_message, error_message1, error_message2) else: homecoming userbottle userbottle = get_user_bottles('how many bottles?', 'the value must integer between 1 , 99', 'that value out of bounds, must between 1 , 99') python
Comments
Post a Comment