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
#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


Messages In This Thread
RE: Beginner: Code not work when longer list - by deanhystad - May-19-2023, 11:19 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Upgraded Python: Module no longer found - for Linux Curbie 8 1,389 Mar-05-2025, 06:01 PM
Last Post: Curbie
Question [SOLVED] Upgraded Python: Module no longer found Winfried 1 1,023 Jan-01-2025, 02:43 PM
Last Post: Larz60+
  Can't get graph code to work properly. KDDDC2DS 1 624 Sep-16-2024, 09:17 PM
Last Post: deanhystad
  I can't for the life of me get this basic If statement code to work CandleType1a 8 2,105 May-21-2024, 03:58 PM
Last Post: CandleType1a
  Extending list doesn't work as expected mmhmjanssen 2 1,326 May-09-2024, 05:39 PM
Last Post: Pedroski55
  hi need help to make this code work correctly atulkul1985 5 1,870 Nov-20-2023, 04:38 PM
Last Post: deanhystad
  newbie question - can't make code work tronic72 2 1,487 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 2,828 May-22-2023, 10:39 PM
Last Post: ICanIBB
  Why doesn't this code work? What is wrong with path? Melcu54 7 3,419 Jan-29-2023, 06:24 PM
Last Post: Melcu54
  How to work with list kafka_trial 8 3,667 Jan-24-2023, 01:30 PM
Last Post: jefsummers

Forum Jump:

User Panel Messages

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