← All coding practice

Coding Practice · Beginner · October 13, 2026 · 5 min read

Python Coding Exercise #37: Nephews

Part of the series Python Practice Exercise — part 37

Before you start: Know input() and if, elif and else · Know == for comparing strings

The question

Please write a program which asks for the user’s name. If the name is Huey, Dewey or Louie, the program should recognise the user as one of Donald Duck’s nephews.

In a similar fashion, if the name is Morty or Ferdie, the program should recognise the user as one of Mickey Mouse’s nephews.

Some examples:

Please type in your name: Morty
I think you might be one of Mickey Mouse's nephews.
Please type in your name: Huey
I think you might be one of Donald Duck's nephews.
Please type in your name: Ken
You're not a nephew of any character I know of.

My Solution

name = input("Please type in your name: ")
if name == "Huey" or name == "Dewey" or name == "Louie":
    print("I think you might be one of Donald Duck's nephews.")
elif name == "Morty" or name == "Ferdie":
    print("I think you might be one of Mickey Mouse's nephews.")
else:
    print("You're not a nephew of any character I know of.")

How My Solution Works

1. One group, several names

Each group of nephews has more than one name. The condition has to be true if the name matches any of them, and that’s what the or operator is for.

or joins two or more conditions into one. The whole thing is true if at least one of the conditions is true:

Left sideRight sideleft or right
TrueTrueTrue
TrueFalseTrue
FalseTrueTrue
FalseFalseFalse

So name == "Huey" or name == "Dewey" or name == "Louie" is true when the name is Huey, or Dewey, or Louie. It’s false only when the name matches none of the three.

2. Each side is a complete comparison

Notice that every part of the or is a full comparison with its own name ==. You can’t shorten it to name == "Huey" or "Dewey". That’s the most common mistake with or, and it’s explained below.

3. The three branches

The if / elif / else chain has three outcomes:

  • If the name is one of Donald Duck’s nephews, print the Donald Duck message.
  • Otherwise, if the name is one of Mickey Mouse’s nephews, print the Mickey Mouse message.
  • Otherwise, print that the user isn’t a nephew of any character the program knows.

The two groups of names don’t overlap, so the order of the first two branches doesn’t change the result. Here are the examples:

NameDonald’s nephew?Mickey’s nephew?Printed
Hueyyes(skipped)Donald Duck’s nephews
MortynoyesMickey Mouse’s nephews
Kennononot a nephew

One small detail: the messages contain an apostrophe (Donald Duck's). That’s fine, because the strings are wrapped in double quotes, so a single quote inside is just a character.

University Solution

The University Solution is exactly the same as My Solution.

The trap: or "Dewey"

It’s very tempting to write the condition the way you would say it in English: “if the name is Huey or Dewey or Louie”.

if name == "Huey" or "Dewey" or "Louie":
    print("I think you might be one of Donald Duck's nephews.")

This is valid Python, and it doesn’t cause an error. But it doesn’t do what you’d expect: every name, including Ken, is treated as one of Donald Duck’s nephews. Here’s what the program prints for some different names:

NamePrinted
HueyDonald Duck’s nephews
MortyDonald Duck’s nephews
KenDonald Duck’s nephews
(empty)Donald Duck’s nephews

The reason is that Python reads the condition as three separate parts joined by or:

name == "Huey"    or    "Dewey"    or    "Louie"

The first part is a real comparison. But the other two parts are just strings by themselves. When Python uses a value as a condition, any non-empty string counts as True. (This was covered under “truthy and falsy values” in Data Types in Python.) So "Dewey" is always true, which makes the whole or always true, whatever the name is.

The fix is to repeat the comparison on every side: name == "Huey" or name == "Dewey" or name == "Louie". It’s longer, but it’s correct.

A shorter way: in

Repeating name == gets tiring with a long list of names. Python has the in operator, which checks whether a value is one of the items in a collection:

name = input("Please type in your name: ")
if name in ("Huey", "Dewey", "Louie"):
    print("I think you might be one of Donald Duck's nephews.")
elif name in ("Morty", "Ferdie"):
    print("I think you might be one of Mickey Mouse's nephews.")
else:
    print("You're not a nephew of any character I know of.")

The names are written inside brackets, separated by commas. That’s a tuple, a fixed collection of values. (Tuples and lists are explained in Core Data Structures in Python.) name in ("Huey", "Dewey", "Louie") asks “is name one of these three?”. It prints the same output as the or version for every name I tried, and it grows more neatly when you add more names. The exercise is about learning or, so the long version is the one to know first, but in is worth remembering.

Things to notice

Capital letters matter. == on strings is case-sensitive, so huey in lowercase doesn’t match "Huey", and the program says the user isn’t a nephew. The same is true for a stray space: " Huey" with a space in front doesn’t match either.

If you wanted the program to be more forgiving, you can clean up the input first, using .strip() to remove extra spaces and .lower() to convert to lowercase (both are explained in String Methods in Python), and then compare with lowercase names:

name = input("Please type in your name: ").strip().lower()
if name in ("huey", "dewey", "louie"):
    print("I think you might be one of Donald Duck's nephews.")

With this version, huey, HUEY and Huey are all recognised. The exercise only asks for the exact names, so this is optional.

Key takeaways

  • or joins conditions, and the result is true when at least one of them is true.
  • Write a full comparison on both sides: name == "Huey" or name == "Dewey", not name == "Huey" or "Dewey".
  • A non-empty string counts as True in a condition, which is why or "Dewey" is always true.
  • name in ("Huey", "Dewey", "Louie") is a shorter way to check against a list of options.
  • String comparison is case-sensitive and sensitive to spaces. Use .strip() and .lower() to be more forgiving.
  • When two groups of options don’t overlap, the order of the elif branches doesn’t matter.

✦ This is part of my Python Practice Exercise series, where I work through exercises while learning Python and explain each answer in plain language. ✦


Practice Source: This exercise is taken from the Python Programming MOOC 2026 by the University of Helsinki. I’m working through the exercises as part of my Python learning journey and documenting my answers and explanations here.

Tagged: python · conditionals · logical-operators · strings · elif