Python Forum
User input only takes the last number
Thread Rating:
  • 2 Vote(s) - 3 Average
  • 1
  • 2
  • 3
  • 4
  • 5
User input only takes the last number
#11
Hello again. First off let me thank you for all your help.
Now, I went back into my code and took a function out and moved some things around. I have it running again with correct validating errors. Only now, it seems that my calculation has gotten mixed up in the process and I am not returning the value(or only the first). Any suggestions would be awesome.

#   Constant Integer HOURS = 7
HOURS = 7



def valid_float_number(value):
    try:
        float(value)
        return value
    except ValueError:
        print("Please try again.")
        return False


#   Function Real real_number(String prompt)
#       Declare String value
#
#       Display prompt
#       Input value
#       While not value is a real number
#           Input value
#       End While
#       Return value
#   End Function

def real_number(prompt):
    value = ""

    value = input(prompt)
    while not valid_float_number(value):
        value = input(prompt)
        return value

    if float(value) < 0 or value == "":
        print(value, "is less than 0!")
        value = input(prompt)
        return value
    else:
        return True


#   Function Real get_pints_for_hours()
#
#       Display prompt
#       Input Real prompt
#
#       Return user_input
#   End Function

def get_pints_for_hour():
    user_input = float(real_number("Enter pints collected: "))

    return user_input






def get_pints_for_drive():
    pints_collected = [0.0 for x in range(HOURS)]
    counter = 0
    while counter < HOURS:
        pints_collected[counter] = get_pints_for_hour()
        counter += 1
    return pints_collected






def calculate_average_pints(pints_collected_during_drive):
    total_pints = 0
    counter = 0

    while counter < HOURS:
        total_pints += pints_collected_during_drive[counter]
        counter += 1
    return float(total_pints / HOURS)



#   Function display_results(average_pints, mimimum, maximum):
#       print("The average number of pints donated is ", "{:.2f}".format(average_pints))
#       print("The highest pints donated is ", maximum)
#       print("The lowest pints donated is ", minimum)
#   End Function

def display_results(average_pints, minimum, maximum):
    print("The average number of pints donated is ", "{:.2f}".format(average_pints))
    print("The highest pints donated is ", maximum)
    print("The lowest pints donated is ", minimum)


#   Function is_yes_or_no(Real value)
#
#       Return value == "yes" or value == "no"


def is_yes_or_no(value):
    return value == "yes" or value == "Yes" or value == "no" or value == "No"


#   Function Boolean end_yes_or_no(String prompt)
#       Declare String value
#
#       Display prompt
#       Input value
#       While not is_yes_or_no
#           Display prompt
#           Input value
#       If value == "yes" or value == "YES"
#            Return True
#       Else
#            Return False
#           End If
#       End While
#   End Function

def end_yes_or_no(prompt):
    value = ""
    value = input(prompt)
    while not is_yes_or_no(value):
        value = input(prompt)
    if value == "yes":
        return True
    else:
        return False


#   Function prompt_done()
#       Display prompt
#       Input prompt
#
#       Return end_yes_or_no(prompt)
#   End Function

def prompt_done():
    prompt = "Do you want to end program? (Enter no or yes): "
    return end_yes_or_no(prompt)


def main():
    done = False

    while not done:
        pints_collected_during_drive = get_pints_for_drive()
        average_pints = calculate_average_pints(pints_collected_during_drive)
        minimum_pints = min(pints_collected_during_drive)
        maximum_pints = max(pints_collected_during_drive)
        display_results(average_pints, minimum_pints, maximum_pints)
        done = prompt_done()



main()
Reply


Messages In This Thread
User input only takes the last number - by Austin11 - Nov-15-2017, 12:09 AM
RE: User input only takes the last number - by Prrz - Nov-21-2017, 03:28 AM
RE: User input only takes the last number - by Prrz - Nov-21-2017, 04:41 AM
RE: User input only takes the last number - by Prrz - Nov-21-2017, 09:20 PM
RE: User input only takes the last number - by Austin11 - Nov-22-2017, 05:00 PM
RE: User input only takes the last number - by Prrz - Nov-22-2017, 07:13 PM
RE: User input only takes the last number - by Prrz - Nov-23-2017, 08:07 PM
RE: User input only takes the last number - by Prrz - Nov-28-2017, 11:20 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
Question Simulate an answer based on user input [Beginner needs guidance] Bombardini 1 1,366 Nov-12-2022, 03:47 AM
Last Post: deanhystad
  Checking the number of input Chrilo06 3 2,090 Mar-14-2022, 07:31 PM
Last Post: deanhystad
  Print user input into triangle djtjhokie 1 2,483 Nov-07-2020, 07:01 PM
Last Post: buran
  Changing Directory based on user input paulmerton4pope 13 8,317 Aug-14-2020, 11:48 AM
Last Post: GOTO10
  Creating a link that takes the user to a random page card51shor 9 6,329 Jul-06-2020, 05:38 AM
Last Post: card51shor
  how to add the user input from file into list wilson20 8 4,433 May-03-2020, 10:52 PM
Last Post: Larz60+
  Writing a function that changes its answer based on user input SirRavenclaw 2 2,913 Dec-21-2019, 09:46 PM
Last Post: Clunk_Head
  Print the longest str from user input edwdas 5 4,281 Nov-04-2019, 02:02 PM
Last Post: perfringo
  how to add user input to a dictionary to a graph KINGLEBRON 3 3,147 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,584 Mar-23-2019, 06:54 PM
Last Post: Yoriz

Forum Jump:

User Panel Messages

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