4
\$\begingroup\$

Instead of using so many if and elif statements I want to organize the names and ages into a list and when a user input the name and age it will iterate through the list of ages to see if any of them are older and if they are it will then return the name of the people who are older from the list of names. This is what I have so far:

name_character=input("Enter the character's name:")
age_character=int(input("Enter the character's age:"))

#the characters we will be comparing out input to
pippin_age=29
frodo_age=51
gollum_age=589
arwen_age=2901

#comaprison
if age_character<0:
    print("invlaid age.")
#younger than all of them
elif age_character<pippin_age:
    print(name_character,"is",age_character,"years old, and they are younger than Pippin, Frodo, Gollum, and Arwen")
#older than pippin(29) younger than Frodo(51)
elif age_character>pippin_age and age_character<frodo_age:
    print(name_character,"is",age_character,"years old, and they are older than Pippin.")
    print(name_character,"is",age_character,"years old, and they are younger than Frodo, Gollum, and Arwen")
#older than frodo(51) younger than Gollum
elif age_character>frodo_age and age_character<gollum_age:
    print(name_character,"is",age_character,"years old, and they are older than Pippin, and Frodo")
    print(name_character,"is",age_character,"years old, and they are younger than Gollum, and Arwen")
#older than gollum(589) and younger than Arwen(2901)
elif age_character>gollum_age and age_character<arwen_age:
    print(name_character,"is",age_character,"years old, and they are older than Gollum,Pippin, and Frodo")
    print(name_character,"is",age_character,"years old,and they are younger than Arwen")
#older than all of them
else:
    print(name_character,"is", age_character,"years old, and they are older Pippin, Gollum, Frodo, and Arwen")

please guide me through the process as I'm very green first python lab and a lot of people try to explain it in terms I just do not understand. Thanks for the help

\$\endgroup\$

2 Answers 2

2
\$\begingroup\$

Don't treat the other characters specially - just put them in a sequence (a tuple, in this case), and compare them in a loop.

Consider adding a validation loop for the age.

Your code needs to be moved into functions.

invlaid is spelled invalid.

There's a lot more you can do here, but this is a quite basic start.

Suggested

CHARACTERS = (
    (29, 'Pippin'),
    (51, 'Frodo'),
    (589, 'Gollum'),
    (2901, 'Arwen'),
)


def get_age():
    while True:
        try:
            age = int(input("Enter the character's age: "))
            if age > 0:
                return age
        except ValueError:
            pass
        print('Invalid age.')


def get_older(age_character):
    for age, name in CHARACTERS:
        if age_character > age:
            yield name


def get_younger(age_character):
    for age, name in CHARACTERS:
        if age_character < age:
            yield name


def print_comparison(name, age, compare_name, compare):
    names = tuple(compare(age))
    if not names:
        return

    print(name, 'is', age, 'years old, and they are', compare_name, 'than', end=' ')
    *others, last = names
    if others:
        print(', '.join(others), 'and', end=' ')
    print(last)


def main():
    name = input("Enter the character's name: ")
    age = get_age()
    print_comparison(name, age, 'older', get_older)
    print_comparison(name, age, 'younger', get_younger)


main()

Output

Enter the character's name: Zelda
Enter the character's age: f
Invalid age.
Enter the character's age: -368
Invalid age.
Enter the character's age: 30
Zelda is 30 years old, and they are older than Pippin
Zelda is 30 years old, and they are younger than Frodo, Gollum and Arwen
\$\endgroup\$
2
\$\begingroup\$

PEP-8

The Style Guide for Python Code has several recommendations you should follow.

White Space

You should have a space around operators like =, +, and <.

name_character=input("Enter the character's name:")

should be:

name_character = input("Enter the character's name:")

You should have a space after comma in argument lists.

    print(name_character,"is",age_character,"years old, and they are younger than Pippin, Frodo, Gollum, and Arwen")

should be:

    print(name_character, "is", age_character, "years old, and they are younger than Pippin, Frodo, Gollum, and Arwen")

Constants

Value defined by the program that will never change should be identified using UPPER_CASE.

#the characters we will be comparing out input to
pippin_age=29
frodo_age=51
gollum_age=589
arwen_age=2901

should be:

# The characters we will be comparing our input to
PIPPIN_AGE = 29
FRODO_AGE = 51
GOLLUM_AGE = 589
ARWEN_AGE = 2901

Chain Comparisons

Two comparisons involving the same value, joined by an and are more naturally written using Python’s chained comparisons:

elif age_character>pippin_age and age_character<frodo_age:

should be:

elif PIPPEN_AGE < age_character < FRODO_AGE:

Meaningless Comments

#comaprison
#younger than all of them
#older than pippin(29) younger than Frodo(51)
#older than frodo(51) younger than Gollum
#older than gollum(589) and younger than Arwen(2901)
#older than all of them

These comments repeat what the code already says. Worse, you embed the same age values you’ve already given as constants, above. If you were to correct an age value, or say add 5 years to all ages to represent 5 additional years passing, you’d additionally have to correct all the age values in the comments. If you didn’t change the comments, the comments would no longer match what the code says, making debugging harder as the reader might trust what the incorrect comments say.

Instead, use comments to describe what the code cannot easily say, like

# Compare against character ages in ascending order, starting at the youngest.

DRY -vs- WET

Don’t Repeat Yourself (DRY), instead of Writing Everything Twice (WET)

You repeat:

    print(name_character,"is",age_character,"years old, and they are 

far too many times. Factor out that code:

if age_character < 0:
    print("invalid age.")

else:
    print(name_character, "is", age_character, "years old, and they are", end=" ")

    # Compare against character ages in ascending order, starting at the youngest.
    if age_character < PIPPIN_AGE:
        print("younger than Pippin, Frodo, Gollum, and Arwen")
    elif PIPPIN_AGE < age_character < FRODO_AGE:
        print("older than Pippin.")
    elif FRODO_AGE < age_character < GOLLUM_AGE:
        print("older than Pippin, and Frodo")
    elif GOLLUM_AGE < age_character < ARWEN_AGE:
        print("older than Gollum,Pippin, and Frodo")
    else:
        print("older Pippin, Gollum, Frodo, and Arwen")

Side note: the = in , end=" ") denotes a “keyword argument” and is not an “operator”, so the PEP-8 guidelines state it is not surrounded by white space.

Bug

Enter the characters name:Zelda
Enter the character's age:51
Zelda is 51 years old, and they are older Pippin, Gollum, Frodo, and Arwen

Your code is comparing against open ranges only.

  • age < 29
  • 29 < age < 51
  • 51 < age < 589
  • 589 < age < 2901
  • anything else

If the age is exactly 29, 51, 589, or 2901, you will declare the entered age as the oldest. You need to decide how you want to handle exact age matches.


See Reinderien's answer for additional structuring changes, lists, etc.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.