Python Forum
validating user input and loops
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
validating user input and loops
#1
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()
Reply
#2
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
Reply
#3
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.
Reply
#4
Quote:The invalid input gets stuck in a loop
where?
Reply
#5
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?
If it ain't broke, I just haven't gotten to it yet.
OS: Windows 10, openSuse 42.3, freeBSD 11, Raspian "Stretch"
Python 3.6.5, IDE: PyCharm 2018 Community Edition
Reply
#6
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.
Reply
#7
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.
Reply
#8
Thank you guys I figured out what was wrong with my code! This site and you guys are awesome. Thank you again!!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Question Simulate an answer based on user input [Beginner needs guidance] Bombardini 1 1,320 Nov-12-2022, 03:47 AM
Last Post: deanhystad
  Print user input into triangle djtjhokie 1 2,420 Nov-07-2020, 07:01 PM
Last Post: buran
  Changing Directory based on user input paulmerton4pope 13 8,152 Aug-14-2020, 11:48 AM
Last Post: GOTO10
  how to add the user input from file into list wilson20 8 4,359 May-03-2020, 10:52 PM
Last Post: Larz60+
  Writing a function that changes its answer based on user input SirRavenclaw 2 2,849 Dec-21-2019, 09:46 PM
Last Post: Clunk_Head
  Print the longest str from user input edwdas 5 4,199 Nov-04-2019, 02:02 PM
Last Post: perfringo
  how to add user input to a dictionary to a graph KINGLEBRON 3 3,074 Jul-31-2019, 09:09 PM
Last Post: SheeppOSU
  New to Python - tiny coding assistance on user input function and assign to variable Mountain_Duck 1 2,538 Mar-23-2019, 06:54 PM
Last Post: Yoriz
  Extracting list element with user input valve 1 2,600 Mar-11-2019, 07:37 PM
Last Post: Yoriz
  turtle polygon as specified by user input johneven 7 10,824 Mar-02-2019, 10:11 PM
Last Post: johneven

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020