Prelab 4 Due: 8:00AM on 2021-10-08

In our lab, you are going to be writing a program that randomly generates math problems like “3 * 4 + 10” and counts how many of these questions a person can answer correctly in a fixed amount of time (say 30 seconds).

Half of this prelab is experimenting and there is nothing to hand in for these parts (“Guessing game” and “Infinite loops”). For the “Timing” section, answer the two questions (labeled “Question 1” and “Question 2”) on Gradescope.

Guessing game

Look at the Python functions we discussed in class, particularly the number_guessing_game function. Play with it a few times and then look at the code and make sure you understand what it is doing. I’ve also provided two alternative functions number_guessing_game2 and number_guessing_game3, which behave the same way, but are structured differently. Look at these variants and make sure you understand those as well. Any of these ways of implementing this function is a good choice.

Timing

Python has a module named time that has a variety of functionality for working with time, dates, etc. If you’re curious, you can check out the documentation. In this lab, we’ll be experimenting with the time function within this module. This function returns the number of seconds that have elapsed since a particular date and time. For example, the following:

>>> import time
>>> print(time.time())


will print the number of seconds elapsed since the starting date.

Question 1: Figure out what date the clock started counting on. To do this, convert the number of seconds that have elapsed into the number of days that have elapsed. Then, use this online tool to subtract that number of days from the current date.

We won’t be using them right now, but there are other functions that allow you to get the current time in a more human friendly way. You can look at the documentation for the time module for more information.

Elapsed time from an arbitrary time/date may not seem that useful. However, we can do easily figure out how long a particular activity takes with that data. We first invoke time() and record how many seconds have elapsed in a variable, then, at some later point after some time has elapsed, invoke time() again and record the result in another variable. If we take the difference between these two time readings, we get the time elapsed.

Question 2: Write a set of Python statements that uses the input function to ask the user for their name, measures the how long it takes them to enter their name, then prints out how many seconds elapsed. You’ll have to record the time before and after the user enters the text.

Infinite loops

for loops run for a fixed number of iterations and so we don’t have to worry about them not finishing. With a while loop, however, if the Boolean loop condition never becomes False then the loop will never end. For example, the following is an infinite loop and will NEVER end:

while True:
print("hello")


In Thonny, create a new file and enter this text, then run the program. What happens? What should happen? A rhetorical question…

Most likely you’ll see a lot of “hello” appear in the Shell. To stop the program, click into the console window, then type Ctrl-c (Control + c). It might take a while to respond, but you should get your prompt back eventually. If Ctrl-c doesn’t work, you can also click on the red “stop sign” icon at the top of Thonny. This will “restart” the Shell process terminating the current computation.

Remember these ways of stopping a program in case you introduce an infinite loop by mistake. If your program doesn’t print anything in the loop, it might just appear to hang, and you’ll have to interrupt it as described above.