Skip to content

Instantly share code, notes, and snippets.

@joetechem
Last active May 16, 2017 15:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joetechem/4e7ecf044400b4c906085aaa3b6af5ec to your computer and use it in GitHub Desktop.
Save joetechem/4e7ecf044400b4c906085aaa3b6af5ec to your computer and use it in GitHub Desktop.
A simple I/O python program with a while loop.
"""
A simple program asking a user to guess the "computer's" favorite color.
The favorite color of the computer is set in a variable, favColor.
The computer checks the user's input to match the favColor variable.
If it is any color other than the computer's favorite color, the loop continues.
Otherwise, the loop is ended when the flag is set to False (or, the user enters the
exact string equal to the favColor variable.
This will be a given program that students in Game Design will execute. Once executed, they
will be prompted with a challenge to create their own program which models the concepts in
the fav_color program.
Objectives:
handling user input
variables review
conditions statements/blocks
while loop
stopping a loop w/ a variable
"""
favColor = 'orange'
print("Hi!")
Questions = True
while Questions:
question = raw_input("\nGuess my favorite color: ")
if question != favColor:
print("Wrong!")
print("Please, try again :)")
elif question == favColor:
print("\nHey...")
print("That is my favorite color!")
raw_input("\n\nPress the enter key to exit")
Questions = False
"""
Below is the same program with descriptive comments.
"""
# The "computer's" favorite color set as a variable.
favColor = 'orange'
print("Hi!")
# Setting a flag variable.
# This acts as a signal to the program. As long as the flag is True, the loop will continue.
Questions = True
# The while loop
while Questions:
# Prompting the user for input. The input is automatically put in a variable for the program to check later.
question = raw_input("\nGuess my favorite color: ")
# Checking if the input (question) is NOT equal to favColor
if question != favColor:
print("Wrong!")
print("Please, try again :)")
# The flag is still true, so the loop continues...
# Else-If satement to check if the input is exactly equal to FavColor
elif question == favColor:
print("\nHey...")
print("That is my favorite color!")
# The flag is set to False, so the loop is ended
Questions = False
# Another example of taking user input (which exits the program)
raw_input("\n\nPress the enter key to exit")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment