Selection Statements
Overview
This week's lab will cover the following:
- Using conditional logic (if statements).
- Applying comparison operators.
- Validating user input.
Lecture Slides
Creating your Github Repo for Lab 4
Use the following link to set up a GitHub repository for Lab 4. Open your Lab 4 repository in GitHub Codespaces, and open the provided template lab4.py. Update the comment block with the appropriate information for name, date, and usage.
Importing the random and sys modules
Python has a built-in module called random that can be used to generate random numbers. The sys module provides allows Python to share information with the Operating System. You will use the sys module to exit with an error. To use these modules, you need to import them into your script.
import random, sys
While you could hardcode a number to guess, making it random will make your game repeatable. The following code will generate a random number (between 1 and 10) and store it in the variable called number.
number = random.randint(1, 10)
Prompting the User for Input
Use the input function to prompt the user to guess the number. Store this in the variable guess
guess = input("Guess a number between 1 and 10: ")
Conditional Logic (if statements)
You can use if statements in code to make decisions based on the value of a variable. If the condition is met, you can execute a block of code. You can use elseif (elif) to check multiple conditions, and else to execute code if none of the conditions are met. It is considered best practice to use a single if block to check a single condition. You can have multiple if blocks, and even nested if blocks (as you will learn in the lab). In this case, you want to compare the user's guess to the random number. If the guess is correct, you will display a message. If the guess is incorrect, you will display a different message.
Validating the User Input
Any time you accept user input, you should validate it. In this case, you want to ensure the user has entered a number. You can use the isnumeric method to check if the input is a number. If it is not, you can display an error message and exit the script. If it is numeric, you will need to convert it to an integer (since you will be performing a mathematical comparison on it later).
if guess.isnumeric():
guess = int(guess)
else:
sys.exit("Invalid input. Please enter a number between 1 and 10.")
Comparing the User's Guess to the Random Number
Next, you should check to see the number the user entered is in the desired range (1-10). You can use an if statement to check this. If it is outside of that range, you will use sys.exit to provide an error message and exit.
if guess < 1 or guess > 10:
sys.exit("Invalid input. Please enter a number between 1 and 10.")
Else, the input is in the valid range and it is a number. You can safely proceed to use an if statement to compare the user's guess to the random number. If the guess is correct, display a message. If the guess is incorrect, display a different message.
if guess == number:
print("You guessed the correct number!")
else:
print("You guessed the wrong number. The correct number is: ", number)
You will revist this script in Lab 6, and add iterative logic (loops) to make your game replayable.
Completing the Lab
Your script should produce output similar to the following (note the sample output contain both a correct and incorrect guess). If it does not, go back and complete whatever you've missed.
Exploration Questions
The following questions are for furthering your knowledge only, and may appear on quizzes or tests at any time later in this course.
- What is the difference between if, elif, and else?
- What is the sys module used for?
- What is the random module used for?