Sean Breckenridge Sean Breckenridge - 3 months ago 13
Python Question

Syntax error and is there a better way to write this?, with if - else conditional

I have just started learning Python, and I'm trying to self-learn it so I can test out of computer programming, to get to java, which is what I really want to learn. I know this isn't the easiest way to do this, but it's what I came up with. When I run it, there's a syntax error, but I don't really see it. I'm pretty sure I have a lot of things wrong with this.

Might someone help me with my syntax please?

Also, if someone could suggest some way to learn Python, or how they learnt it, because I might get a book on it or something.


File "Triangletest.py", line 8 else tyy == 0

^ SyntaxError: invalid syntax


T1 = input("First side of triangle: ")
T2 = input("Second side of triangle: ")
addi = T1 + T2
suub = T1 - T2
T3 = input("Third side of triangle: ")
tyy = 1
else tyy == 0
if T3 < suub:
pss == 1
else pss = 0
if tyy + pss == 2:
print("The triangle is not possible")

Answer

Comments:

T1 = input("First side of triangle:  ")
T2 = input("Second side of triangle:  ")
addi = T1 + T2
suub = T1 - T2
T3 = input("Third side of triangle:  ")
    tyy = 1 \ Identation does not work
else tyy == 0 # else requires a : at the end of the line and an if before it it never has a condition. use elif instead of else: if ...:
    if T3 < suub:
pss == 1 # you mus ident behind every :
    else pss = 0 # this is ok but : missing
if tyy + pss == 2: # there mus be something behind it.

Syntax ok:

T1 = input("First side of triangle:  ")
T2 = input("Second side of triangle:  ")
addi = T1 + T2
suub = T1 - T2
T3 = input("Third side of triangle:  ")
tyy = 1
if False: pass # if before else
elif tyy == 0:
    if T3 < suub:
        # should it be pss = 1 ?
        pss == 1 # this is in the if clause
    else: pss = 0
if tyy + pss == 2:
    pass # do something
Comments