CS 150 - Prelab 4

Due: Friday 3/10 at the beginning of lab

For our lab, we're 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 playing with a few things and there is nothing to hand in for these parts ("Guessing game", "A demo" and "Infinite loops"). For the "Timing" section, answer the two questions (labeled 1 and 2) and bring your answers on a piece of paper to class on Friday.

Guessing game

Look at the class notes for Wednesday on the course web page and look at 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 a function number_guessing_game2, which behaves the same way, but is structured a bit differently. Look at this variant and make sure you understand it as well. Either way of implementing this function is fine.

A demo

(Optional, but strongly suggested)

To get you familiar with what you'll be implementing for your assignment this week, I've provided you with an example program to run. Play with it a little bit before Friday. Note this will only work on the desktop machines in the Mac lab, so you'll need to stop by MBH 505. To run it:


Python has a module called time that has a variety of functionality for asking questions about time, dates, etc. If you're curious, you can check out the documentation. In this lab, we'll be playing with the time function. The time function gives the number of seconds that have elapsed since a particular date and time. For example, you can type:
import time
and you will get the number of seconds elapsed since the starting date.
  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, go to http://www.timeanddate.com/date/dateadd.html and use this online tool to subtract that number of days from the current date. Put the answer to this question on a piece of paper to submit for your prelab. Include both the number of days you calculated, and the resulting date.
We won't be using it 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.

Timing user feedback

Elapsed time from an arbitrary time/data may seem like a weird measurement, however, one useful thing we can do easily is figure out how long a particular activity takes. We record how many seconds have elapsed in a variable using time(), then, at some later point after some time has elapsed, record the time again using time() in another variable. If we take the difference between these two time readings, we get the time elapsed.
  1. Write a set of statements that uses the input function to ask the user for their name, measures the time 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. Put the answer to this question on a piece of paper to submit for your prelab.

Infinite loops

for loops run for a fixed number of iterations and we don't have to worry about them not finishing. With a while loop, however, if the Boolean expression never becomes False then the loop will never end. For example, the following is an infinite loop and will NEVER end:
while True:
In Spyder, create a new file and enter this text, then run the program. What happens? What should happen? (rhetorical questions)

Most likely you'll see a lot of "hello" appear in the console. 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 orange triangle in the top right corner of the console window. This will "kill" the console process, so you'll have to restart it by clicking on the green triangle that appeared. You can also open a new console from the Consoles menu.

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.