Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.6k views
in Technique[技术] by (71.8m points)

why do I get syntax error for else statement in python 2.7?

I used the code below in python 3.9 and it runs with no problem at all but when I use it in 2.7 version it doesnt work I retyped the code 5 times and still doesnt work. it gives me syntax error for the else

Please help me with this

for row in range(7):
    for col in range(11):
        if ((col==0 or col==6 or col==10) or (row==3 and col==1))or((row==2) and (col==2 or col==8)) or (row==4 and col==2) or ((row==1) and (col==3 or col==7 or col==9)) or (row==5 and col==3) or ((row==0 or row==6) and (col==4)):
            print("*",end="")
else:
print(end=" ")
    print()
question from:https://stackoverflow.com/questions/65859875/why-do-i-get-syntax-error-for-else-statement-in-python-2-7

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

print("something") is the syntax of the python3.

in python2, it is print "something".


Add the following code to the first line of the code, then you can use print(...) in python2.7:

from __future__ import print_function

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...