If, Elif, Else¶
Type this one in and make it work, too.
Name your file:
if_elif_else.py
team_a_points = 25
team_a_wins = 15
team_b_points = 20
team_b_wins = 16
if team_a_points > team_b_points:
print("Team A wins!")
team_a_wins += 1
elif team_b_points > team_a_points:
print("Team B wins!")
team_b_wins += 1
else:
print("Tie.")
if team_a_wins > team_b_wins:
print("Team A has more wins than Team B.")
elif team_b_wins > team_a_wins:
print("Team B has more wins than Team A.")
else:
print("Both Teams A and B have the same number of wins.")
What You Should See¶
Team A wins!
Both Teams A and B have the same number of wins.
What You Should Do on Your Own¶
Assignments turned in without these things will receive half credit or less.
Why do you suppose the output says
"Both Teams A and B have the same number of wins."
whenteam_a_wins
is initialized as only15
andteam_b_wins
is initialized as16
? It seems Team B has more wins. What is going on?What do you think
elif
andelse
are doing? Answer in a comment.Pick one of the
elif
statements and change it toif
instead. What difference does that make? Why? Answer in a comment.
©2021 Daniel Gallo
This assignment is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 United States License.
Adapted for Python from Graham Mitchell’s Programming By Doing