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
#17
(Nov-24-2017, 11:41 PM)Austin11 Wrote: I changed up my code to take away the extra while loop and include it in the first while statement when the user puts in a negative it works great. However, when they put in a string I receive a ValueError like below. I have a feeling it has to do with my is_actual_float function but I can't seem to figure out where. I tried changing things around but it didn't work.

Traceback (most recent call last):
  File "C:/Users/Eman/Desktop/lab5_2/lab5.py", line 82, in <module>
    main()
  File "C:/Users/Eman/Desktop/lab5_2/lab5.py", line 75, in main
    pints_collected = get_pints_for_drive()
  File "C:/Users/Eman/Desktop/lab5_2/lab5.py", line 24, in get_pints_for_drive
    pints = float(input("Enter pints collected: "))
ValueError: could not convert string to float: 'Im lost'
Also, I tried your second recommendation with the max and min and for some reason, I keep getting an error message whether it be the format you gave me or the python shortcuts. I know your format is correct along with the python shortcuts. In short, I just need a quick explanation of what the following error message means. I've looked on the internet and just cannot find a concrete answer.

Traceback (most recent call last):
  File "C:/Users/Eman/Desktop/lab5_2/lab5.py", line 82, in <module>
    main()
  File "C:/Users/Eman/Desktop/lab5_2/lab5.py", line 77, in main
    maximum = max(pints_collected)
TypeError: 'float' object is not iterable

For the ValueError, this is indeed a problem with your is_actual_float function, or rather a problem with it not being able to check for a string using the Try and Except method reliably. To fix this is easy enough, remove the "try" and "except" from the is_actual_float function and move it to your get_pints_for_drive function as follows:
        try:
            pints = int(input("Enter pints collected: "))
            if is_actual_float(pints):
                counter += 1
                pints_collected += pints
                pints_data.append(pints)
                pints_data.sort()
            else:
                print("Please try again.")
        except ValueError:
            print("Please try again.")
As for the min and max error, the error you are getting is because floats are not iterable, and max() is trying to iterate through pints_collected, which in my script is a float, not a list, you'd have to call max(pints_data) for that to work. (Floats are not iterable)

I've done this for my own testing and everything seems to be working perfectly, this obviously isn't the complete code just snippets of what is relevant. Side note, I would use [-1] to call the last number in our list instead of [6].

def get_pints_for_drive(pints_data = [], pints_collected = 0):
    # Python Stuff
    return pints_collected, pints_data

def get_average(pints_collected):
    average_pints = round(pints_collected / 7,2)
    return average_pints
def get_max(pints_data):
    max_pints = pints_data[-1]
    return max_pints
def get_min(pints_data):
    min_pints = pints_data[0]
    return min_pints

def display_information(average,maximum,minimum):
    print(average,maximum,minimum)


def main():
    done = False
    while not done:
        pints_collected, pints_data = get_pints_for_drive()
        average = get_average(pints_collected)
        maximum = get_max(pints_data)
        minimum = get_min(pints_data)
        display = display_information(average,maximum,minimum)
        done = prompt_done()
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 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,307 Nov-12-2022, 03:47 AM
Last Post: deanhystad
  Checking the number of input Chrilo06 3 2,034 Mar-14-2022, 07:31 PM
Last Post: deanhystad
  Print user input into triangle djtjhokie 1 2,407 Nov-07-2020, 07:01 PM
Last Post: buran
  Changing Directory based on user input paulmerton4pope 13 8,119 Aug-14-2020, 11:48 AM
Last Post: GOTO10
  Creating a link that takes the user to a random page card51shor 9 6,195 Jul-06-2020, 05:38 AM
Last Post: card51shor
  how to add the user input from file into list wilson20 8 4,355 May-03-2020, 10:52 PM
Last Post: Larz60+
  Writing a function that changes its answer based on user input SirRavenclaw 2 2,838 Dec-21-2019, 09:46 PM
Last Post: Clunk_Head
  Print the longest str from user input edwdas 5 4,190 Nov-04-2019, 02:02 PM
Last Post: perfringo
  how to add user input to a dictionary to a graph KINGLEBRON 3 3,063 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,535 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