2015-07-11 28 views
0
angle1 = int(input('Please enter the 1st angle:')) 
angle2 = int(input('please enter the 2nd angle:')) 
angle3 = int(input('please enter the 3rd angle:')) 
Angle = angle1 + angle2 + angle3 
while Angle == 180: 
    if angle1 and angle2 and angle3 < 90: 
     print ('this an actue triangle') 

    elif angle1 or angle2 or angle3 == 90: 
     print ('this is a right triangle') 

    elif angle1 or angle2 or angle3 > 90: 
     print ('this is an obtuse triangle') 

    Angle = angle1 + angle2 + angle3  
    angle1 = int(input('Please enter the 1st angle:')) 
    angle2 = int(input('please enter the 2nd angle:')) 
    angle3 = int(input('please enter the 3rd angle:')) 

我试图比较每个角度与条件,但似乎每当我在角度3输入数字,它只会比较条件,忽略其他两个角度。请在这件事上给予我帮助!我怎样才能比较蟒蛇中的两个以上的条件

回答

0

我认为你必须给每个单独的角度比较喜欢这个

if (angle1 < 90) and (angle2 < 90) and (angle3 < 90) 

相同的其他条件。但我认为你必须使用或代替if语句。因为当角度是180时,所有三个角度都不能小于90,并且你只是想测试三个角度之一是否小于90.所有条件必须是真实的。

+0

谢谢你完美的工作。 – Isaac

1

您可以使用anyall函数。

ask = lambda: [int(input('Please enter the {0}st angle:'.format(i))) for i in range(1,4)] 
angles = ask() 
while sum(angles) == 180: 
    if all(a < 90 for a in angles): 
     print ('this an actue triangle') 

    elif any(a == 90 for a in angles): 
     print ('this is a right triangle') 

    elif any(a > 90 for a in angles): 
     print ('this is an obtuse triangle') 

    angles = ask() 

编辑:用于Python初学者一些评论:

第一行中,我使用的λ表达式是一个单行的功能。在lambda表达式中,我使用了列表理解(一种紧凑的方式来构建列表)。

因此ask()返回包含3个角度的列表例如[90, 45, 45]

你可以找到有关sum()any()all()here的信息。

+0

你的一些功能对我来说是新的,我无法使用,因为我还没有学习它,但是谢谢你帮助我。 – Isaac

+0

@Isaac好吧,我明白了。有一些有用的内置功能。这是学习的场合! – clemtoy