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
#12
For example, and for your own sake please do not copy this in its entirety, here is the code that I made to solve the very same problem. Run it, read through it. I'll look into the code you've provided, however like I mentioned earlier the format is a bit difficult for me to follow but I'll do my best to help debug it (this will take me some extra time, I'll get back to you on it). If you do decide to follow the model I've given to you, I suggest re-writing it in your own style, because I reiterate, copy-and-pasting would do you no good. With that being said, are there any stipulations that were given by your instructor that I should know about? For instance, things you can and cannot do? Minumum requirements etcetera?

# ! Python3
def get_pints():
    pints_collected = 0
    pints_data = []
    counter = 0
    done = False
    while counter <= 6 and done == False: # counter <= #, # + 1 = number of inputs accepted before calcs
            try:
                pints = int(input("Enter pints collected: ")) # Accept user input
                if pints < 0: # Check if negative
                    print("You've entered a Negative number.")
                else: # If not, add the number to the total and the list
                    pints_collected += pints # Adds input to total
                    pints_data.append(pints) # Appends input into a list
                    counter += 1 # Adds 1 to counter
                    print(pints_data) # Only for testing purposes, delete later
                    print(pints_collected) # Only for testing purposes, delete later
            except ValueError: # If not a number
                print("That is not a valid entry, try again please.")
    else:
        # 7 is number of inputs taken, change if (counter <= #) is changed
        average = round(pints_collected / 7) 
        maximum = max(pints_data)
        minimum = min(pints_data)
        print("The average number of pints donated is: ",average)
        print("The highest pints donated is ",maximum)
        print("The lowest pints donated is ",minimum)
        retry = input("End the program? (Yes or No): ").upper() # Eliminates case errors in input
        if retry == "NO":
            pints_collected = 0 # Reset total
            pints_data = [] # Reset list
            counter = 0 # Reset counter
            return get_pints() # Return original function
        else:
            print("Goodbye!")
            done = True
get_pints() # Start program by calling get_pints function
Regards,
Prrz
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,336 Nov-12-2022, 03:47 AM
Last Post: deanhystad
  Checking the number of input Chrilo06 3 2,054 Mar-14-2022, 07:31 PM
Last Post: deanhystad
  Print user input into triangle djtjhokie 1 2,441 Nov-07-2020, 07:01 PM
Last Post: buran
  Changing Directory based on user input paulmerton4pope 13 8,220 Aug-14-2020, 11:48 AM
Last Post: GOTO10
  Creating a link that takes the user to a random page card51shor 9 6,260 Jul-06-2020, 05:38 AM
Last Post: card51shor
  how to add the user input from file into list wilson20 8 4,400 May-03-2020, 10:52 PM
Last Post: Larz60+
  Writing a function that changes its answer based on user input SirRavenclaw 2 2,875 Dec-21-2019, 09:46 PM
Last Post: Clunk_Head
  Print the longest str from user input edwdas 5 4,231 Nov-04-2019, 02:02 PM
Last Post: perfringo
  how to add user input to a dictionary to a graph KINGLEBRON 3 3,104 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,555 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