$30
COMP 1005/1405
1
Assignment 2
Advanced branching and looping
Submit a single zip file called assignment2.zip. This assignment has 30 marks. See the
marking rubric that is posted on the course webpage (also includes test cases). Sample outputs
for each question are included at the end of this document.
Problem 1 (Car Stop/Go)
For this question, you will write two programs that determine if a car should go through an
intersection or stop. Your program will prompt the user for the colour of the traffic light at the
intersection (a string), the distance to the intersection in meters (a float), and the speed of the
car in meters per second (a float). The following rules should govern the car’s decision:
1. “Go” when the light is “green”
2. When the light is “yellow”, “Go” if the car can reach the intersection within 5
seconds. Otherwise, the car must “Stop”
3. When the light is “red”, “Go” if the car can reach the intersection in 2 seconds.
Otherwise, the car must “Stop”
4. If the light is any color other than “green”, “red”, or “yellow”, the car must
“Stop”
a) Write a program that prints out the proper command (“Go” or “Stop”) based on the given
inputs using only nested if and else statements (i.e., do not use elif, and, or, not,
etc.). For example, your program should have a structure similar to:
if boolean_statement1:
if boolean_statement2:
#code block 2a
else:
#code block 2b
else:
if boolean_statement3:
#code block 3a
else:
#code block 3b
b) Write a program that prints out the proper command (“Go” or “Stop”) based on the given
inputs using a single if/else statement with compound Boolean expressions. In this
case, your program must have a structure like:
if compound_boolean_statement:
#code block 1
else:
#code block 2
COMP 1005/1405 – S17 – A2 Due Sunday, May 21 at 11:55 PM
2
Save your two programs as a2q1a.py and a2q1b.py respectively and add each to your
submission zip file.
Problem 2 (Factorial Calculation)
Write a program that calculates the factorial value of a number entered by the user. Remember
that x! = x * (x-1) * (x-2) * ... * 3 * 2 * 1. Your program should check the value input by the user
to ensure it is valid (i.e., that it is a number = 1). To do this, consider looking at the isdigit()
function available in Python. If the user enters an incorrect input, your program should continue
to ask them to enter a proper value until they do so, at which point the program can calculate
the factorial and print out the result.
Note: You must do the calculation manually – you cannot use a built-in factorial function.
Save your Python program in a file called a2q2.py and add it to your submission zip file.
Problem 3 (Tracking Numbers)
Write a program that reads in numbers from the user. Your program should be capable of
tracking/calculating the minimum, maximum, total sum, and average of the numbers entered.
When the user enters "q", "Q", or "quit", the program should display the minimum, maximum,
total sum, and average of the numbers entered before ending. You can assume that the user
will always enter either a number or one of the strings “q”, “Q”, or “quit”. You can also assume
the user will always enter at least one number (i.e., they will not immediately choose to quit
upon starting the program).
Save your Python program in a file called a2q3.py and add it to your submission zip file.
Recap
Your zip file should contain your a2q1a.py, a2q1b.py, a2q2.py, and a2q3.py files.
Submit your assignment2.zip file to cuLearn.
Make sure you download the zip after submitting and verify the file contents.
COMP 1005/1405 – S17 – A2 Due Sunday, May 21 at 11:55 PM
3
Sample Results for Each Question
Question #1
What color is the traffic light? green
How fast is the car going? 0.5
How far to the intersection? 10
The car should go.
What color is the traffic light? orange
How fast is the car going? 5.5
How far to the intersection? 1
The car should stop.
What color is the traffic light? yellow
How fast is the car going? 5
How far to the intersection? 27
The car should stop.
What color is the traffic light? yellow
How fast is the car going? 4.5
How far to the intersection? 14
The car should go.
Question #2
Enter a number you want the factorial of: 5
The factorial of 5 is 120
Enter a number you want the factorial of: lala
Invalid input.
Enter a number you want the factorial of: -5
Invalid input.
Enter a number you want the factorial of: 3
The factorial of 3 is 6
Enter a number you want the factorial of: 120
The factorial of 120 is
6689502913449127057588118054090372586752746333138029810295671352301633557244
9629893668741652719849813081576378932140905525344085894081218598984811143896
50005964960521256960000000000000000000000000000
COMP 1005/1405 – S17 – A2 Due Sunday, May 21 at 11:55 PM
4
Question #3
Enter a number (q, Q, or quit to exit): 15
Enter a number (q, Q, or quit to exit): 10
Enter a number (q, Q, or quit to exit): 25
Enter a number (q, Q, or quit to exit): 5
Enter a number (q, Q, or quit to exit): 0
Enter a number (q, Q, or quit to exit): 30
Enter a number (q, Q, or quit to exit): q
Min = 0
Max = 30
Total = 85
Average = 14.166666666666666
Enter a number (q, Q, or quit to exit): -57
Enter a number (q, Q, or quit to exit): quit
Min = -57
Max = -57
Total = -57
Average = -57.0
Enter a number (q, Q, or quit to exit): 0
Enter a number (q, Q, or quit to exit): 2
Enter a number (q, Q, or quit to exit): 4
Enter a number (q, Q, or quit to exit): 6
Enter a number (q, Q, or quit to exit): 8
Enter a number (q, Q, or quit to exit): 10
Enter a number (q, Q, or quit to exit): Q
Min = 0
Max = 10
Total = 30
Average = 5.0
Enter a number (q, Q, or quit to exit): -100
Enter a number (q, Q, or quit to exit): 100
Enter a number (q, Q, or quit to exit): 37
Enter a number (q, Q, or quit to exit): -256
Enter a number (q, Q, or quit to exit): 1024
Enter a number (q, Q, or quit to exit): 9
COMP 1005/1405 – S17 – A2 Due Sunday, May 21 at 11:55 PM
5
Enter a number (q, Q, or quit to exit): 93
Enter a number (q, Q, or quit to exit): 93
Enter a number (q, Q, or quit to exit): 16
Enter a number (q, Q, or quit to exit): quit
Min = -256
Max = 1024
Total = 1016
Average = 112.88888888888889