Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
else: sytax error
#1
Hello,
I ha been following an example on lesson letter by letter of the code but els comes out as invalid systax.
here is the code.
#multi Exception

a=10
b=2
i=3
x=5
try:
    c=a/x
except NameError:
    c=a/i
print("Variable is not yet created")
    else:
    x=100
    print(x)
This is suppose to print
100
since there is a condition of Except NameError:
the code must proceed to print (x)
Reply
#2
Works fine for me. prints 100. You should put all code in bb tags and post any errors
Python uses indents for code blocks.
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags


Reply
#3
In a try... except... else, the else must be indented at the same level as try and except. Also the indentation of the print... line is wrong because if it starts at the beginning of the line, it means that the try block is already finished. It should be indented like c=a/i
Reply
#4
Did it!
thanks for your guide...



(Jul-23-2023, 03:58 PM)Gribouillis Wrote: In a try... except... else, the else must be indented at the same level as try and except. Also the indentation of the print... line is wrong because if it starts at the beginning of the line, it means that the try block is already finished. It should be indented like c=a/i
Reply


Forum Jump:

User Panel Messages

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