Python Forum
else: sytax error - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: else: sytax error (/thread-40404.html)



else: sytax error - EddieG - Jul-23-2023

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)


RE: else: sytax error - menator01 - Jul-23-2023

Works fine for me. prints 100. You should put all code in bb tags and post any errors
Python uses indents for code blocks.


RE: else: sytax error - Gribouillis - Jul-23-2023

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


RE: else: sytax error - EddieG - Jul-24-2023

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