Lab 3
Objectives
- Measure elapsed time with
time.time() - Collect values into a list across repeated calls to a function
- Practice with loops, string concatenation, and use of the
randommodule
Part 0: Setup
Download the file lab3.zip by clicking the blue “Download Starter Code” button above; move the zip file to your cs145/labs directory, then double-click on the zip file to create a subfolder called lab3 inside your labs directory.
Part 1: Timing
Python has a time module that has a variety of functionality for working with time, dates, etc. In this part of the lab, we’ll experiment with the time() function within this module, which returns the number of seconds elapsed since January 1, 1970. For example, at the time this assignment was written,
>>> import time
>>> time.time()
1788022158.116338Elapsed time from an arbitrary starting date may not seem useful. However, we can easily figure out how long a particular activity takes with that data. We first invoke time() and record the result in a variable, then, at some later point after some time has elapsed, invoke time() again and record the result in another variable. The difference between these two time readings is the elapsed time.
Question 1: Complete ask_name() (in lab3_part1.py), which uses input() to ask the user for their name, measures how long it takes them to enter it, then returns how many seconds elapsed. Store the time before and after the user enters text, then return the difference:
def ask_name():
"""
Time how long it takes the user to enter their name
Parameters: None
Returns:
float: number of seconds it took user to enter their name
Example:
>>> ask_name()
Enter your name: Amy Briggs
2.211042881011963
"""Question 2: Complete time_entries(n) (in lab3_part1.py), which calls ask_name() n times and collects each elapsed time into a list, in the order they were collected:
def time_entries(n):
"""
Call ask_name() n times and collect each elapsed time into a list.
Parameters:
n (int): number of times to call ask_name()
Returns:
list of float: the n elapsed times, in the order collected
Example:
>>> time_entries(2)
Enter your name: Amy Briggs
Enter your name: Amy Briggs
[2.211042881011963, 1.822917938232422]
"""Try it out (no coding required): Call time_entries(3) from the Shell and store the result in a variable, e.g. times = time_entries(3). Then try min(times) and max(times) – these built-in functions work on any list of numbers, and return the smallest/largest value in the list. Keep this pattern in mind – it comes back in Homework 3.
Part 2: Generating random equations
In class we worked on a function to generate a random arithmetic problem using +, -, and * as possible operators, and numbers 1 through 10 (inclusive) as possible operands. One way to generate a random equation:
generate a random number between 1 and 10 to start the equation
for each operator you want to add:
pick a random operator from +, -, * and add it to the equation
pick a random number between 1 and 10 and add it to the equation
Using this approach, you can generate a random equation with as many operators as you like.
Complete random_equation(num_op) (in lab3_part2.py), which takes the number of operators to generate and returns a string representing a random math equation using the numbers 1-10 and the operators +, -, * (no 0, since it makes multiplication too easy, and no division, since it can produce floating-point results that are impossible to guess). Your function should work for zero operators too:
def random_equation(num_op):
"""
Generate random equation of form 'num op num op ... num'.
Parameters:
num_op (int): number of operators
Returns:
str: equation as a string
"""Example runs:
>>> random_equation(4)
'5 - 6 - 7 * 6 * 4'
>>> random_equation(1)
'3 * 7'
>>> random_equation(2)
'8 - 5 * 2'
>>> random_equation(7)
'8 + 7 + 4 - 7 - 2 * 3 * 2 * 4'
>>> random_equation(0)
'4'You’ll need a loop to get the repetition (for or while) – think about which is more appropriate here. Also notice the equation is a string, so you’ll be building it up the same way you did in the last assignment, by appending additional pieces.
Submitting your work
When you are done, submit these two files to Gradescope:
lab3_part1.pylab3_part2.py
Ensure that the upload succeeds and your submission passes all visible tests.