-2

In this code if a person if greater than 80 he/she should be displayed with different statement whereas the one with age greater than 18 different but if possible without adding anymore if statements

if  80 > given > 18:
    print("you are eligible for voting")
else:
    print("you are not")
python_ged
  • 496
  • 8
  • 1
    Please describe in more detail what output you desire. – timgeb May 07 '21 at 07:43
  • I don't get what you are asking. You want to take more conditions into account without taking more conditions into account? Are you just looking for tricks to replace an ``if`` *statement* with something equivalent, such as an ``if`` *expression*? – MisterMiyagi May 07 '21 at 07:44
  • [Does Python have a ternary conditional operator?](https://stackoverflow.com/questions/394809/does-python-have-a-ternary-conditional-operator) – khelwood May 07 '21 at 07:45

1 Answers1

2

Possible to do without ifs at all!

for age in (10, 19, 50, 88):
    print('age', age, 'is', ['<18', '18:80', '>80'][
        int((age - 18) / (80 - 18) + 1)])

Output:

age 10 is <18
age 19 is 18:80
age 50 is 18:80
age 88 is >80
Natallia
  • 66
  • 4