语法错误,是否有更好的方法来写这个?,if - else条件

时间:2013-11-01 01:48:25

标签: python if-statement conditional

我刚开始学习Python,我正在尝试自学,所以我可以测试计算机编程,去java,这是我真正想学的东西。我知道这不是最简单的方法,但这是我想出来的。当我运行它时,会出现语法错误,但我并没有真正看到它。我很确定我有很多错误。

有人可以用我的语法帮助我吗?

此外,如果有人可以建议某种方式来学习Python,或者他们是如何学习它的,因为我可能会得到一本关于它的书或其他东西。

  

文件“Triangletest.py”,第8行其他tyy == 0
              ^ SyntaxError:语法无效

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")

2 个答案:

答案 0 :(得分:0)

评论:

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.

语法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

答案 1 :(得分:0)

似乎你正试图解决这个问题:用户进入侧面,程序会回答是否可以使用这些边的三角形。如果你对你的方面进行排序,那将会更加简单。

sides = [float(input(ord + ' side of a triangle: '))
             for ord in 'First Second Third'.split()]
a, b, c = sorted(sides)
if c < a + b: print('Triangle is possible')
else: print('Triangle is impossible')