-2

I have a code snippet inline which has to be modified so that it uses one print statement.

    age=12
    if age < 18:
      if age < 12:
        print('kid')
      else:
        print('teenager')
    else:
      print('adult')

I tried to approach this problem by putting if conditions in a single print statement without use of extra variable.

    age=12
    print('kid' if age<18 and age<12 else 'teenager' if age<18 and age>=12 else 'adult')

The result of the modified code snippet is coming same as that of the original code snippet but wanted to confirm if its the right approach according to the question or should i use an extra variable and store the outcome of each if statement and print the variable at the end of if condition.

2 Answers2

2

I think you should review the main ideals of python. If we open a console and import this we will see:

"""
The Zen of Python, by Tim Peters
Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!
"""

Where of special note is Readability counts. and Flat is better than nested.. If you need to use only a single print() then you should use a variable to keep the result and then just print the variable. This will maintain readability of your code. Something like:

age=12
if age <= 12:
    stage_of_life = 'kid'
elif 12 < age < 18:
    stage_of_life = 'teenager'
else:
    stage_of_life = 'adult'

print(stage_of_life) # only one print statement in code
Reedinationer
  • 5,661
  • 1
  • 12
  • 33
  • in elif condition i changed it to range(12,18) would it be in accordance to the ideals of python? – Ten Doeschate Apr 08 '19 at 17:32
  • @TenDoeschate I do not think that you want to create a range object in this situation. That would return a generator for the numbers listed, namely `12, 13, 14, 15, 16, 17` (not as a list though!). Typically `range()` is used to define a variable on each iteration of a `for` loop as in `for i in range(12, 18)`;`print(i)` – Reedinationer Apr 08 '19 at 17:36
  • ok got it so can we use age<18 in elif condition for me 12 – Ten Doeschate Apr 08 '19 at 17:42
  • My apologies I had `=<` and it should have been `<=` this may have been the error – Reedinationer Apr 08 '19 at 17:48
0

Well you asked for it, here are my two cents:

ages = [5, 12, 19]

def get_age_status(age):
    return 'Kid' if age < 12 else 'Teenager' if age < 18 else 'Adult'

for age in ages:
    print(get_age_status(age))

Outputting:

Kid
Teenager
Adult
Kunal Mukherjee
  • 5,775
  • 3
  • 25
  • 53