Nov-02-2017, 03:50 PM
So I am new to programming and I need to create a program that will allow the user to input how many bottles they collected over seven days. ie: Monday = 1, Tuesday = 5.... and then calculate the total number bottles collected and the amount of money they received (each bottle goes for 10 cents) I understand that part but when it comes to the validating the user's input I am lost. The invalid input gets stuck in a loop even when the user inputs a whole number. No error message is showing which makes it a bit hard to understand. Any help is greatly appreciated!
def is_valid_integer(input_string): try: val = int(input_string) is_valid = True except ValueError: is_valid = False return is_valid def total_bottles_returned(): input_string = "" mon = input("How many bottles did you return on Monday: ") is_valid = is_valid_integer(input_string) while not is_valid: mon = input("Nice try, please enter a whole number! ") is_valid = is_valid_integer(input_string) input_integer = int(mon) tue = input("How many bottles did you return on Tuesday: ") is_valid = is_valid_integer(input_string) while not is_valid: input_string = input("Nice try, please enter a whole number! ") is_valid = is_valid_integer(input_string) input_integer = int(input_string) wed = input("How many bottles did you return on Wednesday: ") is_valid = is_valid_integer(input_string) while not is_valid: input_string = input("Nice try, please enter a whole number! ") is_valid = is_valid_integer(input_string) input_integer = int(input_string) thu = input("How many bottles did you return on Thursday: ") is_valid = is_valid_integer(input_string) while not is_valid: input_string = input("Nice try, please enter a whole number! ") is_valid = is_valid_integer(input_string) input_integer = int(input_string) fri = input("How many bottles did you return on Friday: ") is_valid = is_valid_integer(input_string) while not is_valid: input_string = input("Nice try, please enter a whole number! ") is_valid = is_valid_integer(input_string) input_integer = int(input_string) sat = input("How many bottles did you return on Saturday: ") is_valid = is_valid_integer(input_string) while not is_valid: input_string = input("Nice try, please enter a whole number! ") is_valid = is_valid_integer(input_string) input_integer = int(input_string) sun = input("How many bottles did you return on Sunday: ") is_valid = is_valid_integer(input_string) while not is_valid: input_string = input("Nice try, please enter a whole number! ") is_valid = is_valid_integer(input_string) input_integer = int(input_string) return input_string, mon, tue, wed, thu, fri, sat, sun def calculate_returned_bottles(mon, tue, wed, thu, fri, sat, sun): price_per_bottle = .10 bottles_mon = int(mon) * price_per_bottle bottles_tue = int(tue) * price_per_bottle bottles_wed = int(wed) * price_per_bottle bottles_thur = int(thu) * price_per_bottle bottles_fri = int(fri) * price_per_bottle bottles_sat = int(sat) * price_per_bottle bottles_sun = int(sun) * price_per_bottle print("Monday pay out is: ", bottles_mon) print("Tuesday pay out is: ", bottles_tue) print("Wednesday pay out is: ", bottles_wed) print("Thursday pay out is: ", bottles_thur) print("Friday pay out is: ", bottles_fri) print("Saturday pay out is: ", bottles_sat) print("Sunday pay out is: ", bottles_sun) return bottles_mon,bottles_tue,bottles_wed,bottles_thur,bottles_fri,bottles_sat,bottles_sun def calculate_final_bottle_price(bottles_mon, bottles_tue, bottles_wed, bottles_thur, bottles_fri ,bottles_sat, bottles_sun): monday = bottles_mon tuesday = bottles_tue wednesday = bottles_wed thursday = bottles_thur friday = bottles_fri saturday = bottles_sat sunday = bottles_sun final_bottle_price = (monday + tuesday + wednesday + thursday + friday + saturday + sunday) print("Total returned for the week: ", final_bottle_price) def main(): input_string, mon, tue, wed, thu, fri, sat, sun = total_bottles_returned() bottles_mon, bottles_tue, bottles_wed, bottles_thur, bottles_fri, bottles_sat, bottles_sun = calculate_returned_bottles(mon, tue, wed, thu, fri, sat, sun) calculate_final_bottle_price(bottles_mon, bottles_tue, bottles_wed, bottles_thur, bottles_fri ,bottles_sat, bottles_sun) main()