Let’s try some simple practice problems. These problems are not graded - they are just for you to practice before doing the real exercises in the lab itself. You can find the answers below each question by clicking the button below each question.
4.11 Reading Code
Consider the following Python program:
p1 = int(input("Enter a positive integer for player 1: "))
p2 = int(input("Enter a positive integer for player 2: "))
p3 = int(input("Enter a positive integer for player 3: "))
if p1 <= 0 or p2 <= 0 or p3 <= 0:
print("Error")
else:
twos = (p1 + p2 + p3) // 2
threes = (p1 + p2 + p3) // 3
fours = (p1 + p2 + p3) // 4
if p1 == p2 or p2 == p3 or p3 == p1:
print("Tie")
elif p1 != twos and p2 != threes and p3 != fours:
print("Draw")
else:
if p1 == twos:
print("Player 1 Wins")
if p2 == threes:
print("Player 2 Wins")
if p3 == fours:
print("Player 3 Wins")
Explain, in your own words, a set of rules for the game that this program is simulating.
A fully correct answer is a succinct set of rules for the game and what leads to the various outcomes. A partially correct answer is a step-by-step description of each line in the program and the output it will produce based on the input.
Hint: it is possible for multiple players to win a round in this game.
4.12 Testing Code
Consider the Python program in the question above. Give a set of inputs that will achieve branch coverage for this program. You do not have to achieve path coverage!
Hint: try 3, 2, 1
as one possible input. What branches will that execute?
4.13 Writing Code
Write a complete Python program that meets the specification below.
A childcare supervisor has developed a set of rules to determine if children under their care may play indoors or outdoors. Here are the rules:
- If it is raining, then the children must play indoors since they’ll get wet. This rule takes precedence over all other rules.
- If the temperature is below 0° C, then the children must play indoors because it is too cold, unless there is snow on the ground. In that case, children may play both indoors and outdoors.
- If the temperature is above 40° C, then the children must play indoors because it is too hot, unless the pool is open. In that case, children may play both indoors and outdoors.
- If the temperature is between 20° C and 30° C, then the children must play outdoors because it is a nice day.
- Otherwise, if no other rules are matched, children may choose to play both indoors and outdoors.
Write a program that will help determine if the children may play indoors, outdoors, or both. The program will prompt the user for four inputs in the following order:
- The temperature as an integer (measured in degrees Celsius).
- Whether it is raining:
yes
orno
. - Whether there is snow on the ground:
yes
orno
. - Whether the pool is open:
yes
orno
.
The program should output either indoors
, outdoors
, or both
, depending on the rules described above. Your program must make use of chained and/or nested conditional statements.
Hint: after reading input, the yes
and no
answers can be converted to Boolean values once and stored in Boolean variables. This will simplify the Boolean expressions used in the conditional statements.