0

I just started learning Python at Uni and am a little confused on how to properly link two lists.

I need to write a program that uses inputed users name and age and compares it to a list of names and ages and prints a line such as "Eric is X years old and is older than (input all names of those younger" + same thing for those who are younger.

Im unsure how to link each name to an age then use this to print out only the names of those who are older/younger.

A proper printout would look like:

Ezekiel is 25 years old, and is younger than Bob and Zac.

Ezekiel is 25 years old, and is older than John, Eric, Tim, George.

We are not allowed to use dictionaries. Thanks.

name = input("What is your name of your character? ")
age = int(input("How old is your character? "))

names = ["John",
    "Eric",
    "Bob",
    "Tim",
    "George",
    "Zac",]
ages = [59, 39, 12, 80, 26, 20,]

for items in ages:
    if age > items:
        print(f'{name} is {age} years old, and they are younger than {items}')
Zak Smith
  • 1
  • 1
  • See [zip](https://docs.python.org/3.3/library/functions.html#zip), e.g. `for item_name, item_age in zip(names, ages):`. – Mark Tolonen Jan 25 '23 at 20:08
  • Alternatively, you could have a single list of people in your source code (tuples / named tuples) – Lesiak Jan 25 '23 at 20:11

1 Answers1

0

If you can't use dictionaries, you can use the index of the lists.

For example, you can use enumerate and list comprehension

Solution:

for index, age in enumerate(ages):
    olders = [names[i] for i, other_age in enumerate(ages) if other_age > age]
    print(f"{names[index]} is {age} years old, and they are younger than {', '.join(olders)}")
FedeG
  • 106
  • 6
  • Another solution is to use **zip**, but this function returns an iterable with tuples (merge two lists in one list of tuples with elements joined by index) – FedeG Jan 25 '23 at 20:19