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


Messages In This Thread
validating user input and loops - by Austin11 - Nov-02-2017, 03:50 PM
RE: validating user input and loops - by Larz60+ - Nov-02-2017, 04:09 PM
RE: validating user input and loops - by Austin11 - Nov-02-2017, 04:51 PM
RE: validating user input and loops - by Larz60+ - Nov-02-2017, 07:25 PM
RE: validating user input and loops - by Austin11 - Nov-02-2017, 08:11 PM
RE: validating user input and loops - by nilamo - Nov-02-2017, 08:34 PM
RE: validating user input and loops - by Austin11 - Nov-03-2017, 12:24 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
Question Simulate an answer based on user input [Beginner needs guidance] Bombardini 1 1,423 Nov-12-2022, 03:47 AM
Last Post: deanhystad
  Print user input into triangle djtjhokie 1 2,528 Nov-07-2020, 07:01 PM
Last Post: buran
  Changing Directory based on user input paulmerton4pope 13 8,510 Aug-14-2020, 11:48 AM
Last Post: GOTO10
  how to add the user input from file into list wilson20 8 4,524 May-03-2020, 10:52 PM
Last Post: Larz60+
  Writing a function that changes its answer based on user input SirRavenclaw 2 2,980 Dec-21-2019, 09:46 PM
Last Post: Clunk_Head
  Print the longest str from user input edwdas 5 4,382 Nov-04-2019, 02:02 PM
Last Post: perfringo
  how to add user input to a dictionary to a graph KINGLEBRON 3 3,202 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,625 Mar-23-2019, 06:54 PM
Last Post: Yoriz
  Extracting list element with user input valve 1 2,671 Mar-11-2019, 07:37 PM
Last Post: Yoriz
  turtle polygon as specified by user input johneven 7 11,066 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