Home > Net >  Can anyone explain how to solve this problem
Can anyone explain how to solve this problem

Time:11-10

A program that reads 3 numbers A, B and C and checks if each 3 numbers are greater than or equal to 20. Output should be single line containing a boolean. True should be printed if each number is greater than or equal to 20, Otherwise False should be printed.

I have tried using "and" operator and got result. Are there any other ways to solve this problem.

A=int(input())
B=int(input())
C=int(input())

a= A>=20
b= B>=20
c= C>=20

abc= a and b and c

print(abc)

CodePudding user response:

You can use the all function with a generator expression that iterates over a range of 3 to test if each input value is greater than or equal to 20:

print(all(int(input()) >= 20 for _ in range(3)))

CodePudding user response:

This is another way:

abc = all(a, b, c)

CodePudding user response:

Take the lowest thanks to the min() function.

If the lowest value is >= 20 then you're sure that all the values are >= 20.

A = 21
B = 22
C = 19

min(A,B,C) >= 20  # False

CodePudding user response:

What about :

a = A>=20
b = B>=20
c = C>=20

sum((a, b, c))==3
  • Related