How can I compare more than 2 condition in python -
angle1 = int(input('please enter 1st angle:')) angle2 = int(input('please enter 2nd angle:')) angle3 = int(input('please enter 3rd angle:')) angle = angle1 + angle2 + angle3 while angle == 180: if angle1 , angle2 , angle3 < 90: print ('this actue triangle') elif angle1 or angle2 or angle3 == 90: print ('this right triangle') elif angle1 or angle2 or angle3 > 90: print ('this obtuse triangle') angle = angle1 + angle2 + angle3 angle1 = int(input('please enter 1st angle:')) angle2 = int(input('please enter 2nd angle:')) angle3 = int(input('please enter 3rd angle:'))
i tried compare each angle condition seem whenever enter number in angle3, compare condition , ignore other 2 angle. please me this!
you can use any
, all
functions.
ask = lambda: [int(input('please enter {0}st angle:'.format(i))) in range(1,4)] angles = ask() while sum(angles) == 180: if all(a < 90 in angles): print ('this actue triangle') elif any(a == 90 in angles): print ('this right triangle') elif any(a > 90 in angles): print ('this obtuse triangle') angles = ask()
edit: comments python beginners:
first line, used lambda expression one-line function. in lambda expression, used list comprehension (a compact way build list).
so ask()
returns list containing 3 angles e.g. [90, 45, 45]
.
you can find information sum()
, any()
, all()
here.
Comments
Post a Comment