1. Anuncie Aqui ! Entre em contato fdantas@4each.com.br

[Python] Using functions, for loops, conditionals and lists

Discussão em 'Python' iniciado por Stack, Outubro 4, 2024 às 04:02.

  1. Stack

    Stack Membro Participativo

    I have already successfully wrote a program that a. Accepts the names and ages of three people. b. Prints the name of the oldest person. c. Prints the name of the youngest person. d. Prints their age category: • Teen: Ages 13–19 • Adult: Ages 20–64 • Senior: Ages 65 and above

    Here is my code:

    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


    Now I need to do the same problem, but use functions, for loops, conditionals and lists.

    I have been at this for hours and just cannot figure out what to do. This is what I've written so far:

    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)


    I don't know how to define the variable {name} to pull from my list of names.

    I am using Jupyter Notebooks Python 310 and am a complete beginner to programming.

    Continue reading...

Compartilhe esta Página