使用函数、for 循环、条件和列表

问题描述 投票:0回答:1

我已经成功编写了一个程序 一个。接受三个人的姓名和年龄。 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

现在我需要做同样的问题,但是使用函数、for 循环、条件和列表。

我已经在这个问题上呆了好几个小时了,只是不知道该怎么做。这是我到目前为止所写的:

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,并且是一个完全的编程初学者。

python python-3.x
1个回答
0
投票

你走在正确的轨道上,但你可以进一步减少事情。

这里最有用的事情之一是使用数据结构......或更简单地说,一组相关项目。丢失了可用于表示关系的容器,基本容器是列表、元组和字典。

例如,您可以将数据转换为元组列表。

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: ")) 

people = [
    (person1_name, person1_age),
    (person2_name, person2_age),
    (person3_name, person3_age),
]

现在,当引用一个人时,您可以使用此列表,其中列表中的每个项目都是

(name, age)
的元组。

Python 有很多方法可以处理像这样的简单数据结构。

例如,sorted 方法允许您根据其属性重新排列列表,而

itemgetter
方法允许轻松阅读的方式来查询它所查看的每个数据结构的属性。

from operator import itemgetter

# sort values by index 1 in the tuple... the age!
people = sorted(people, key=itemgetter(1))

youngest_name, youngest_age = people[0]
print(f"{youngest_name} is the youngest, they are {youngest_age} years old")

oldest_name, oldest_age = people[-1]
print(f"{oldest_name} is the oldest, they are {oldest_age} years old")

与其让

age_category_function
打印值,不如让它返回要使用的名称。这将减少重复,并允许您在更多上下文中调用它。

def get_age_category(age):
    if age < 13:
        return "Child"
    elif age < 21:
        return "Teen"
    elif age < 65:
        return "Adult"
    else:
        return "Senior"

现在你可以这样称呼它:

for name, age in people:
    age_category = get_age_category(age)
    print(f"{name} is a {age_category}.")

希望这对您有帮助!如果您需要更多解释,请告诉我!

© www.soinside.com 2019 - 2024. All rights reserved.