Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
python beginner
#1
can any tell me what i am doing wrong please

num = 0
tot = 0.0
while true:
  sval = input('Enter a number:')
  if sval == 'done!':
      break
try:
      fval= float(sval)
except:

    print('invalid input')
    continue

num = num+1
tot = tot + fval

print(tot,num, tot/num)
ERROR SAYS

SyntaxError: 'continue' not properly in loop

PLEASE HELP.
Reply
#2
Like it says, the continue on line 12 is not in a loop. That is because it is not indented under the loop. It may look like it is, because it is indented further than the loop statement is. But the loop ends when the indentation for it ends on line 7. The continue statement is only indented under the except statement, not the loop.

What I think you want to do is indent lines 7 through 15 one level. Unfortunately, your levels are not consistent. It looks like you should indent those lines two spaces each.

Once that is working, you should go back and make all of your indentation levels consistent (the same number of spaces). Four spaces is pretty standard in the Python community, but three everywhere is better than two here, four there, and five elsewhere. Any decent text editor should have a setting for handling this.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
for indentation do i need to use TABkeyword or spaces, by the way i'm using Atom text editor.
thanks for you help!
İmage
Reply
#4
I notice "true" in while loop is not "True", Try this to see if it helps,

num = 0
tot = 0.0
while True:
	sval = input('Enter a number:')
	if sval == 'done!':
		break
	try:
		fval= float(sval)
	except:
		print('invalid input')
		continue
 
num = num+1
tot = tot + fval
 
print(tot,num, tot/num)
Best Regards,
Sandeep

GANGA SANDEEP KUMAR
Reply


Forum Jump:

User Panel Messages

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