Python Forum

Full Version: validating user input and loops
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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()
Are you allowed to use python lists?
If so this code can be reduced dramatically.

to keep the current format, you can put main into a loop similar to:
def main():
    no_good = True # Set to true to force first iteration
    max_value = 10.0
    while no_good:
        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)
        if calculate_final_bottle_price(bottles_mon, bottles_tue, bottles_wed, bottles_thur, bottles_fri ,bottles_sat, bottles_sun)  <= max_value:
            no_good = False
I don't know what constitutes OK (not stated), so used a max amount.
Change this for your needs
We haven't used the list method in class but briefly went over them. I began to attempt to use a list but I wanted to figure out why the program is running the error input code even when the user's input was correct. Also, I didn't know how to call each input from a list so I avoided it for the time being.

Looking at your code, I am a bit confused. Would creating a loop in the main fix the code repeating where it shouldn't? I thought that my code in the total_bottles_returned function was creating it but I just can't figure out where.

Sorry, I'm not doubting you, I am just a bit confused right now that's all.
Quote:The invalid input gets stuck in a loop
where?
In line 11 of the above code, you define "input_string" as an empty string, where do you update it (prior to calling "is_valid_integer")?  What other variable might you use as an argument when calling that function?

If you have not covered lists, or you are confused by what you see, do not use it in your code. What will you do when your teacher asks you to explain to the class what you did and why you did it?
After the user inputs how many bottles they returned for Monday, Tuesday etc. But if they were to input anything other than an integer for their return, it should display "Nice try, please enter a whole number. " this is where it gets stuck in the loop. It only displays "Nice try, please enter a whole number." For the rest of the program even after the input is correct.
Quote:
    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)

You're checking if input_string is a number, but you never set input_string, so it'll never be a number.  Instead, you set mon, but then never use mon for anything.

That's why you're stuck in the loop.
Thank you guys I figured out what was wrong with my code! This site and you guys are awesome. Thank you again!!