Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python Code error
#1
Hello Friends:

Can someone please help me and tell me what is wrong with follwing code of mine. After entering the input, system is coming out of the loop and executing last two lines.

count = 0
tot = 0

while True :

    num = input('Enter a number: ')

    if num == 'Done':
        break

    try:
            nm = float(num)
            count = count+1
            tot = tot + nm

    except:
            print ('Invalid Entry')
            continue

print(tot/count)
Reply
#2
I don't understand your problem. I ran the code and here's the output
Output:
Enter a number: 5 Enter a number: Done 5.0
If this not your expected output, then can you please explain what are you trying with the code?
pyzyx3qwerty
"The greatest glory in living lies not in never falling, but in rising every time we fall." - Nelson Mandela
Need help on the forum? Visit help @ python forum
For learning more and more about python, visit Python docs
Reply
#3
Hellos Sir,

I was trying to get the average of numbers entered in the loop. My code was not working before but later it worked.

Thank you for your reply.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Code error from Fundamentals of Python Programming van Richard L. Halterman Heidi 12 1,601 Jul-25-2023, 10:32 PM
Last Post: Skaperen
  Syntax error while executing the Python code in Linux DivAsh 8 1,450 Jul-19-2023, 06:27 PM
Last Post: Lahearle
  Compiles Python code with no error but giving out no output - what's wrong with it? pythonflea 6 1,468 Mar-27-2023, 07:38 AM
Last Post: buran
  Error in if-then-else python code Led_Zeppelin 6 2,284 Jul-27-2022, 07:53 PM
Last Post: deanhystad
  Facing error while executing below Python code ramu4651 1 5,630 Jan-26-2021, 06:40 PM
Last Post: ibreeden
  Skeleton file export error Python Code pepapoha 4 3,426 Nov-17-2020, 02:06 AM
Last Post: pepapoha
  Compiling Python 3.8.5 source code results in build error Deepan 0 2,143 Sep-14-2020, 04:11 AM
Last Post: Deepan
  Syntax error in python code sample ErnestTBass 5 3,059 Aug-14-2020, 07:14 PM
Last Post: deanhystad
  Error in Python Code ErnestTBass 4 2,670 Jun-04-2020, 05:28 PM
Last Post: buran
  Simple python code error cls0724 5 3,270 Mar-12-2020, 07:45 PM
Last Post: stullis

Forum Jump:

User Panel Messages

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