Python Forum
Beginner: Code not work when longer list
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Beginner: Code not work when longer list
#1
Hello,

I been watching videos from python for everyone and there was this code.

numlist = list()
while True:
    inp = input("Enter a number:")
    if inp == 'done' : break
    value = float(inp)
    numlist.append(value)
avarage=sum(numlist) / len(numlist)
print('Avarage: ', avarage)
It runs fine for while and then it would give this error at some points when entering new number:

Error:
Traceback (most recent call last): File "c:\Users\...\Desktop\PYton\numlist.py", line 5, in <module> value = float(inp) ^^^^^^^^^^ ValueError: could not convert string to float: ''
Just wondering why that happens.
Thanks for help.
Raivis.
buran write May-19-2023, 08:18 AM:
Please, use proper tags when post code, traceback, output, etc. This time I have added error tags for you.
See BBcode help for more info.
Reply
#2
(May-19-2023, 08:14 AM)raiviscoding Wrote: Just wondering why that happens.
It's not because of the list length, but because there is empty string '' and it cannot convert that to float. Obviously you just hit Enter at some point.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
Instead of checking for "done", check if len(inp) == 0 or if inp == "". Now you just print <Enter> without entering a number to exit.

Or you could exit on any non-number input.
numbers = list()
try:
    while True:
        numbers.append(float(input("Enter a number:")))
except ValueError:  # Raised by float(str) when str is not a numeric string.
    print(f"Average{numbers} = {sum(numbers) / len(numbers)}")
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  hi need help to make this code work correctly atulkul1985 5 800 Nov-20-2023, 04:38 PM
Last Post: deanhystad
  newbie question - can't make code work tronic72 2 696 Oct-22-2023, 09:08 PM
Last Post: tronic72
  Why do I have to repeat items in list slices in order to make this work? Pythonica 7 1,355 May-22-2023, 10:39 PM
Last Post: ICanIBB
  Why doesn't this code work? What is wrong with path? Melcu54 7 1,809 Jan-29-2023, 06:24 PM
Last Post: Melcu54
  How to work with list kafka_trial 8 2,048 Jan-24-2023, 01:30 PM
Last Post: jefsummers
  Code used to work 100%, now sometimes works! muzicman0 5 1,449 Jan-13-2023, 05:09 PM
Last Post: muzicman0
  color code doesn't work harryvl 1 896 Dec-29-2022, 08:59 PM
Last Post: deanhystad
  Something the code dont work AlexPython 13 2,267 Oct-17-2022, 08:34 PM
Last Post: AlexPython
  a longer docstring Skaperen 8 1,687 Aug-25-2022, 11:21 PM
Last Post: Skaperen
  cannot get code to work Led_Zeppelin 10 2,466 Jun-30-2022, 06:28 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

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