If Else Statements

If-else statements are the simplest decision-making tool. It is a way for the programmer to tell the computer to execute different parts of the code based on what is happening in the program execution. This means the program can output different results every time it is run!

Blocks of code in an if-else statement can only be executed if a certain condition is met. To check to see if a condition is met, the program will evaluate a comparison statement(this will make more sense once you see an example), and if it evaluates to True, the block of code will run. If False, maybe a different block of code will run, or the code in the if-else statement might be skipped entirely, depending on what the programmer wrote.

Here is a rock-paper-scissors game that can be played against a computer!

								
import random
choices = ["rock", "paper", "scissors"]
print("Rock crushes scissors. Scissors cut paper. Paper covers rock.")
player = input("Do you want to be rock, paper, or scissors (or quit)? ")                
player = player.lower()             
computer = random.choice(choices)  
print("You chose " + player + ", and the computer chose " + computer + ".")
if player == computer:
		print("It's a tie!")
elif player == "rock":
		if computer == "scissors":
			print("You win!")
		else:
			print("Computer wins!")
elif player == "paper":
		if computer == "rock":
			print("You win!")
		else:
			print("Computer wins!")
elif player == "scissors":
		if computer == "paper":
			print("You win!")
		else:
			print("Computer wins!")
else:
	print("I think there was some sort of error...")

								
							

This program has a couple of things you might not have seen before. Firstly, random is a Python library that allows the computer to psudeorandomly select a random number, or random item from a list, or anything else. In this case, the random library is used to randomly pick a choice for the computer to play(computer = random.choice(choices)), from the list of option provided at the top of the program.

After explaining the game to the user, asking the user for what they want to play(rock, paper, or scissors), and selecting a choice for the computer to play, there are a couple of if-else statements to determine the outcome of the game. The first one checks to see if the two choices are the same, and if they are, then the computer will say that the game was a tie.

The next three conditionals start with elif, which means "else if". All three of them check for different choices the player could have picked. For each choice, there is a nested conditional that checks to see if the computer picked the losing choice. Depending on the choices of the computer and the player, the program will say who won. The very last statement is an else statement, which just means that if no other conditions were met, do this. In this case, if the player did not choose any of the given options, then the program throw an error message.

Small Quiz Program

Your goal for this program is to make a program that gives the user a quiz to play and checks their answers.

  1. Create a variable to keep track of correct answers.
  2. Ask a question and take user input(Ex. What is the capital of Germany?)
  3. Check their user input(Ex. If they say “Munich” they are correct and add a point to their score, if they say anything else, they are wrong)
  4. Keep track of how many questions they answered right and print their score at the end

Here is an example of what it should look like:

Additional Resources