The Mood Face Program

This is an example mini-program to demonstrate the use of an elif statement.

The program generates a random number between 1 and 3 and then prints out a given mood face.

Work through the three sections.

1. The Random Mood Program

# Mood Computer
# Demonstrates the elif clause
import random
print("Right now I am feeling...")

mood = random.randint(1, 3)

if mood == 1:
  # happy
  print( \
  """
-----------
|         |
| O     O |
|    <    |
|         |
| .     . |
|  `...`  |
-----------
  """)
elif mood == 2:
  # neutral
  print( \
  """
-----------
|         |
| O     O |
|    <    |
|         |
| ------- |
|         |
-----------
  """)
elif mood == 3:
  # sad
  print( \
  """
-----------
|         |
| O     O |
|    <    |
|         |
|   .'.   |
|  '   '  |
-----------
  """)

input("\n\nPress the enter key to exit.")

You'll note that because the multiline strings do not have indentation, the lines,

  print( \
  """
-----------
|         |
| O     O |
|    <    |
|         |
| .     . |
|  `...`  |
-----------
  """)

are actually really one line of code, but the multiline string is split across the 10 lines.

2. Refactoring the Program

If you look carefully you will notice that all the faces have the same first 5 lines. and the same last line.

-----------
|         |
| O     O |
|    <    |
|         |
-----------

Therefore we can just print this out once and then print out the bottom 3 lines depending on the mood.

# Mood Computer
# Demonstrates the elif clause
import random
print("Right now I am feeling...")

# print the first five lines of the face
print("""-----------
|         |
| O     O |
|    <    |
|         |""")

mood = random.randint(1, 3)

# use elif to print mood lines of the face
if mood == 1:
  # happy
  print("| .     . |")
  print("|  `...`  |")
elif mood == 2:
  # neutral
  print("| ------- |")
  print("|         |")
elif mood == 3:
  # sad
  print("|   .'.   |")
  print("|  '   '  |")

# print the bottom line of the face
print("-----------")
input("\n\nPress the enter key to exit.")

This is exactly the same program but slightly shorter.

3. Accept User Input

We make one final change to the program to ask the user for a number between 1 and 3 instead of randomly generating the number.

# Mood Computer
# Demonstrates the elif clause
mood = int(input("Please enter a number between 1 and 3:\n"))

# use elif to print mood lines of the face
if (mood < 1) or (mood > 3):
  print("Illegal mood value!")
else:
  print("Right now I am feeling...\n")

  # print the first five lines of the face
  print("""-----------
|         |
| O     O |
|    <    |
|         |""")
  
  if mood == 1:
    # happy
    print("| .     . |")
    print("|  `...`  |")
  elif mood == 2:
    # neutral
    print("| ------- |")
    print("|         |")
  elif mood == 3:
    # sad
    print("|   .'.   |")
    print("|  '   '  |")

  # print the bottom line of the face
  print("-----------")

input("\n\nPress the enter key to exit.")

Note now if you enter a number that isn't 1,2 or 3 you will get back,

Illegal mood value!

References

Dawson, M. (2010). Python programming for the absolute beginner, third edition (3rd ed.). Delmar Cengage Learning.