我有一个"学校;我需要帮助解决的任务。
我刚刚学习了函数,所以我需要使用它们。
我有3个人年龄:
- Ola 12岁
- 卡里15岁
- Lise 20岁
我需要使用我制作的功能打印出来,如果年龄在13岁到19岁之间;你是一个青少年&;否则";你不是一个青少年";
这是我现在得到的代码,但我不理解print((。
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")
因为我在控制台上得到的答案是:
You are not a teenager
12 Ola
我只是不确定如何以正确的方式格式化它,我想让它在一行上说:
Ola, you are 12 years old, You are not a teenager
有几种方法可以做到这一点。这里有一个:
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)
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)
试着读一本书或博客来学习基本的功能。
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)
这是我想做的,但还有其他方法