Solutions - Chapter 5

Back to solutions.

5-3: Alien Colors #1

Imagine an alien was just shot down in a game. Create a variable called alien_color and assign it a value of 'green', 'yellow', or 'red'.

Passing version:

alien_color = 'green'

if alien_color == 'green':
    print("You just earned 5 points!")

Output:

You just earned 5 points!

Failing version:

alien_color = 'red'

if alien_color == 'green':
    print("You just earned 5 points!")

(no output)

top

5-4: Alien Colors #2

Choose a color for an alien as you did in Exercise 5-3, and write an if-else chain.

if block runs:

alien_color = 'green'

if alien_color == 'green':
    print("You just earned 5 points!")
else:
    print("You just earned 10 points!")

Output:

You just earned 5 points!

else block runs:

alien_color = 'yellow'

if alien_color == 'green':
    print("You just earned 5 points!")
else:
    print("You just earned 10 points!")

Output:

You just earned 10 points!

top

5-5: Alien Colors #3

Turn your if-else chain from Exercise 5-4 into an if-elif-else cahin.

alien_color = 'red'

if alien_color == 'green':
    print("You just earned 5 points!")
elif alien_color == 'yellow':
    print("You just earned 10 points!")
else:
    print("You just earned 15 points!")

Output for 'red' alien:

You just earned 15 points!

top

5-6: Stages of Life

Write an if-elif-else cahin that determines a person’s stage of life. Set a value for the variable age, and then:

age = 17

if age < 2:
    print("You're a baby!")
elif age < 4:
    print("You're a toddler!")
elif age < 13:
    print("You're a kid!")
elif age < 20:
    print("You're a teenager!")
elif age < 65:
    print("You're an adult!")
else:
    print("You're an elder!")

Output:

You're a teenager!

top

5-7: Favorite Fruit

Make a list of your favorite fruits, and then write a series of independent if statements that check for certain fruits in your list.

favorite_fruits = ['blueberries', 'salmonberries', 'peaches']

if 'bananas' in favorite_fruits:
    print("You really like bananas!")
if 'apples' in favorite_fruits:
    print("You really like apples!")
if 'blueberries' in favorite_fruits:
    print("You really like blueberries!")
if 'kiwis' in favorite_fruits:
    print("You really like kiwis!")
if 'peaches' in favorite_fruits:
    print("You really like peaches!")

Output:

You really like blueberries!
You really like peaches!

top

5-8: Hello Admin

Make a list of five or more usernnames, including the name 'admin'. Imagine you are writing code that will print a greeting to each user after they log in to a website. Loop through the list, and print a greeting to each user:

usernames = ['eric', 'willie', 'admin', 'erin', 'ever']

for username in usernames:
    if username == 'admin':
        print("Hello admin, would you like to see a status report?")
    else:
        print("Hello " + username + ", thank you for logging in again!")

Output:

Hello eric, thank you for logging in again!
Hello willie, thank you for logging in again!
Hello admin, would you like to see a status report?
Hello erin, thank you for logging in again!
Hello ever, thank you for logging in again!

top

5-9: No Users

Add an if test to hello_admin.py to make sure the list of users is not empty.

usernames = []

if usernames:
    for username in usernames:
        if username == 'admin':
            print("Hello admin, would you like to see a status report?")
        else:
            print("Hello " + username + ", thank you for logging in again!")
else:
    print("We need to find some users!")

Output:

We need to find some users!

top

5-10: Checking Usernames

Do the following to create a program that simulates how websites ensure that everyone has a unique username.

current_users = ['eric', 'willie', 'admin', 'erin', 'Ever']
new_users = ['sarah', 'Willie', 'PHIL', 'ever', 'Iona']

current_users_lower = [user.lower() for user in current_users]

for new_user in new_users:
    if new_user.lower() in current_users_lower:
        print("Sorry " + new_user + ", that name is taken.")
    else:
        print("Great, " + new_user + " is still available.")

Output:

Great, sarah is still available.
Sorry Willie, that name is taken.
Great, PHIL is still available.
Sorry ever, that name is taken.
Great, Iona is still available.

Note: If you’re not comfortable with list comprehensions yet, the list current_users_lower can be generated using a loop:

current_users_lower = []
for user in current_users:
    current_users_lower.append(user.lower())

top

5-11: Ordinal Numbers

Ordinal numbers indicate their position in a list, such as 1st or 2nd. Most ordinal numbers end in th, except 1, 2, and 3.

numbers = list(range(1,10))

for number in numbers:
    if number == 1:
        print("1st")
    elif number == 2:
        print("2nd")
    elif number == 3:
        print("3rd")
    else:
        print(str(number) + "th")

Output:

1st
2nd
3rd
4th
5th
6th
7th
8th
9th

top