Использование функций, циклов for, условных операторов и списков. ⇐ Python

Программы на Python
Anonymous
Использование функций, циклов for, условных операторов и списков.

Сообщение Anonymous »

Я уже успешно написал программу, которая
a. Принимает имена и возраст трёх человек.
b. Печатает имя самого старого человека.
c. Печатает имя самого младшего человека.
d. Указывает возрастную категорию:
• Подростки: 13–19 лет.
• Взрослые: 20–64 лет.
• Пожилые люди: 65 лет и старше.
Здесь. это мой код:

Код: Выделить всё

person1_name = input("First person's name: ")
person1_age = int(input("First person's age: "))
person2_name = input("Second person's name: ")
person2_age = int(input("Second person's age: "))
person3_name = input("Third person's name: ")
person3_age = int(input("Third person's age: "))     # Prompting user to enter all 3 names and ages

if person1_age > person2_age and person1_age > person3_age:
oldest = person1_age
print(person1_name + " is the oldest.")
elif person2_age > person1_age and person2_age > person3_age:
oldest = person2_age
print(person2_name + " is the oldest.")
elif person3_age > person1_age and person3_age > person2_age:
oldest = person3_age
print(person3_name + " is the oldest.")          # This block is comparing all 3 ages to see which integer is the largest, then using an if conditional to print a statement if the condition is true

if person1_age < person2_age and person1_age < person3_age:
youngest = person1_age
print(person1_name + " is the youngest.")
elif person2_age < person1_age and person2_age < person3_age:
youngest = person2_age
print(person2_name + " is the youngest.")
elif person3_age < person1_age and person3_age < person2_age:
youngest = person3_age
print(person3_name + " is the youngest.")       # This block is comparing all 3 ages to see which integer is the smallest, then using an if conditional to print a statement if the condition is true

if person1_age in range(13,20):
age_category = "Teen"
print(f"{person1_name} is a {age_category}.")
elif person1_age in range(20,65):
age_category = "Adult"
print(f"{person1_name} is an {age_category}.")
elif person1_age >= 65:
age_category = "Senior"
print(f"{person1_name} is a {age_category}.")
else:
print(f"{person1_name} is not in a valid age category.")   # This block is comparing the person's age to a range of numbers that define an age category, and prints a statement using an if conditional if their age belongs to that age range

if person2_age in range(13,20):
age_category = "Teen"
print(f"{person2_name} is a {age_category}.")
elif person2_age in range(20,65):
age_category = "Adult"
print(f"{person2_name} is an {age_category}.")
elif person2_age >= 65:
age_category = "Senior"
print(f"{person2_name} is a {age_category}.")
else:
print(f"{person2_name} is not in a valid age category.")   # Same as above block but for person2

if person3_age in range(13,20):
age_category = "Teen"
print(f"{person3_name} is a {age_category}.")
elif person3_age in range(20,65):
age_category = "Adult"
print(f"{person3_name} is an {age_category}.")
elif person3_age >= 65:
age_category = "Senior"
print(f"{person3_name} is a {age_category}.")
else:
print(f"{person3_name} is not in a valid age category.")  # Same as above block but for person3
Теперь мне нужно решить ту же задачу, но использовать функции для циклов, условий и списков.
Я занимался этим уже несколько часов и просто не могу понять, что делать. Вот что я написал на данный момент:

Код: Выделить всё

names = []
ages = []

for x in range(0,3):
names.append(input())
ages.append(int(input()))

oldest = max(ages)
print(oldest)

youngest = min(ages)
print(youngest)

def age_category_function(age):
for y in ages:
if age > -1 and age < 13:
age_category = "Child"
print(f"{name} is a {age_category}.")
elif age > 12 and age < 21:
age_category = "Teen"
print(f"{name} is a {age_category}.")
elif age > 19 and age < 65:
age_category = "Adult"
print(f"{name} is an {age_category}.")
elif age >= 65:
age_category = "Senior"
print(f"{name} is a {age_category}.")

age_category_function(age)
Я не знаю, как определить переменную {name}, которую нужно извлечь из моего списка имен.
Я использую Jupyter Notebooks Python 310 и я полный новичок в программировании.

Подробнее здесь: https://stackoverflow.com/questions/790 ... -and-lists

Вернуться в «Python»