Home > Back-end >  How to make a Python function that let me decide if a person is a teenager or not using if statement
How to make a Python function that let me decide if a person is a teenager or not using if statement

Time:10-19

I have a "school" task that i need help to solve.

I have just learned about functions so i need to use them.

I got 3 persons age:

  • Ola 12 years old
  • kari 15 years old
  • Lise 20 years old

I need to print out using a function that i made if the ages is between 13 and 19 years old it should print out "you are a teenager" or else "you are not a teenager"

This is the code i got now, but its the print() that i dont understand.

Ola = 12
Kari = 15
Lise = 20


def age_number(age):
  if age >= 13 and age <= 19:
    print("You are a teenager")
  else: 
    print("You are not a teenager")
  return age

print()
print()
answerola = age_number(Ola)
print(answerola, "Ola")

because the answer i get in the console is:

You are not a teenager
12 Ola

I am just not sure how to format it in the right way, i want it to say on one line:

Ola, you are 12 years old, You are not a teenager

CodePudding user response:

name = 'Ola'
age = 12

def age_number(name, age):
  if (age >= 13 and age <= 19):
    print(f"{name}, you are {age}. You are a teenager")
  else: 
    print(f"{name}, you are {age}. You  are not a teenager")


age_number(name, age)

Try to read a book or blog to learn basics of funtions.

CodePudding user response:

there are several ways of doing this. here is one:

Ola = ['ola', 12]
Kari = ['Kari', 15]
Lise = ['Lise',20]


def age_number(person):

  if person[1] >= 13 and person[1] <= 19:
    print(f"{person[0]} is a teenager")
  else: 
    print(f"{person[0]} is not a teenager")

age_number(Lise)

CodePudding user response:

def age_number(name, age):
 if age >= 13 and age <= 19:
  print(str(name)  " You are "  str(age)   " years old. You are a teenager")
 else: 
  print(str(name)  " You are "  str(age)   "years old.  You are not a 
teenager")
print()
print()
age_number('Ola', 14)

this is what I would do, but there are other ways

  • Related