Lab 4: I'm Thinking of a Number

Sat Oct 17 08:55:52 EDT 2020
Note added 10/17: Please look at the Python examples here, especially They will help you with this lab.

This lab is meant to help you learn the rudiments of the Python programming language and to write a Python program that implements a simple but important algorithm. You will probably also begin to develop some appreciation of why programming is not entirely trivial. It really does require orderly thinking and meticulous attention to detail. On the other hand, it's a great feeling when your program works. Enjoy.

Please read these instructions through before beginning the lab.
Please follow the instructions about program format, variable names, etc.
Please pay attention to syntax and grammar of language constructs.

You can do this lab anywhere, but the lab assistants offer valuable advice not obtainable elsewhere.

Part 1: Introduction
Part 2: The Python language
Part 3: Writing your own Python program
Part 4: Finishing up

Part 1: Introduction

Programming languages provide a way to express computer algorithms in a form that is convenient for humans and can be translated into a computer's instruction repertoire. Programming languages are the way that we tell computers how to perform a task.

In class, we have studied the very low-level instructions that the computer itself understands (for example, the Toy), and talked about a variety of programming languages, easier for people to use, that are translated into native machine instructions by programs like compilers and assemblers. There are many such languages, each with its own good and bad points, and often with noisy adherents and detractors.

Python, the topic of this lab and the next, is one of the most widely used programming languages. It's easy to learn, easy to use, and it comes with an enormous library of useful code for almost any application area you might think of. We don't expect you to become a full-fledged Python programmer, but you will do enough in this lab and the next to get some understanding of what programs look like and what is involved in converting a process into a program.

There is an enormous amount of Python information on the Web, and thousands of books. You might start by searching for "python tutorial" or take a look at Codecademy, which has an interactive walk through Python basics. W3schools is very good for syntax and small examples that you can experiment with.

Python has three major components:

In this lab, we're going to concentrate on programming, not on fancy input and output. This will mean using variables, arithmetic, loops and decisions, and functions, while doing only minimal interaction with the environment, just enough to get data from a user and to display results. The focus is on figuring out what the pieces are and how to combine them into logic that does a computational task.

Although a programming language provides a way for you to tell a computer what to do, you have to spell out the steps in precise detail. You have at your disposal statements for:

Programming languages provide a number of piece-parts that you can use to help create a program quickly; in fact, you couldn't do much without some of these, and it would be very time-consuming to create others from scratch. In effect, these component are like prefabricated components for houses -- windows, doors, kitchen cupboards -- that can be used even by beginners to create polished products.

Some of the components are basic computational units: functions for mathematical operations (computing a square root or generating a random number, for example), or processing strings of characters (like finding or replacing a word within a sentence, or converting between upper and lower case).

Other building blocks let you interact with a user, with the environment on the computer, and with the Internet.

Normally you would run Python on your own computer, and if that's possible for you, it's recommended for this lab. But if not, you can use services like Google's Colab notebook, which provides an interactive web-based environment for running Python code. When Python runs within the confines of a browser, there are some limitations on what you can do. For example, it may not be possible to read information from the file system on your computer or to store information there, but you can usually access anything on the Internet.

Do your programming in small steps, and make sure that each step works before moving on to the next. Pay attention to error messages from the Python compiler; they may be cryptic and unfamiliar, but they do point to something that's wrong.


Part 2: The Python language

Here we will provide a very brief overview of some of the commands and syntax available in Python. This is by no means a complete reference, however, so you will want to look at some of the web sites we suggested above, and review what was covered in class. We are using Python 3. Version 2 is still around (it seems to be the default on macOS) but it doesn't do what we want; in particular the input function that we use for fetching input from a user doesn't work right.

You can test which (if any) version of Python you have in a terminal window (Terminal on a Mac, Powershell on Windows) by typing the command name python or perhaps python3:

$ python
It will respond with something like
Python 3.7.1 (v3.7.1:260ec2c36a, Oct 20 2018, 03:13:28) 
Python 2.7.10 (default, Feb 22 2019, 21:55:15) 
python: command not found
If you don't have Python 3, use Colab.

Displaying text with print

Step 1 is to create and run a minimal program to be sure that you have Python available and can run programs; this is traditionally called Hello World. Here's the simplest version, which calls the function print to display text:

print("Hello, world")
Put this in a file called and run Python:
$ python
Hello, world
This is the basic framework that you will now flesh out -- the single print will gradually be replaced by more and more statements. You should create this file with whatever text editor you used for the earlier labs, using plain text. [Mac TextEdit users: be careful to save as plain text.] Be sure to save the results often, and reload the resulting web page after each change so you can be sure that your changes are working. Frequent small steps is the best strategy for both web page design and programming.

If you are using Colab, you will edit and run your Python code in a browser window. This is described in Chapter 7 of the new edition of Understanding the Digital World. It will look something like this:

Input with the input function

The function input is a sibling to print; it displays a message and waits for you to type a value. Whatever you type is returned as a sequence of characters that can be used in the rest of the program.

reply = input("Type your name: ");
print("Hello", reply);

This sequence of statements declares a variable called reply. It then calls prompt and stores the resulting value (what the user typed) in the variable reply. Then it displays Hello and the reply text, separated by a space.

The detailed behavior of input is a bit subtle. If you press Return or Enter without typing anything, input returns an empty sequence of characters (which is represented in Python as ""). Otherwise, what comes back is whatever the user typed. This sequence will let you test whether the reply was empty:

answer = input("What's your name? ");
if (answer == "":
   # code for an empty answer goes here ...
   # code for a non-empty answer goes here ...


A variable is a name that refers to a place in your program where information is stored; it ultimately corresponds to a location in memory. Variables can hold different types of information, but you don't generally have to worry much about that -- Python handles the details for you. The main types are integers (like 123), floating point numbers (with a decimal point and optional fractional part, like 3.14159), and string (a sequence of characters, like "hello, world" or "3.1415").

Before you can use a variable in a Python program, you must assign a value to it. For example:

first_name = "Joe"
last_name = "College"
GPA = 2.1;
Python distinguishes between upper case and lower case, so gpa and GPA are two different and unrelated variables.

A variable is a place to store a value so it can be used later in a program. To store a particular value in a variable, write variable = value. For example, to save the name from a user, you would write this:

username = input("What's your name? ");
Then you can use the user's name later on in the program by using the variable username.

Quotes and quoting

If you need a literal string of characters in your program, like Hello, there, enclose it in either single ' quotes or double " quotes. If the string contains one kind of quote, you can quote it with the other. If you inadvertently omit a quote or don't balance quotes properly, it's an error and the Python compiler will complain.
Make sure that your editor (e.g., Notepad, TextEdit) preserves the ASCII quote characters (the regular single- and double-quote characters on the keyboard); word processing programs think they are doing you a favor by using so-called "smart" quotes.

Arithmetic Operators, Expressions and Statements

Operators are special symbols like the + and - of ordinary arithmetic. An operator acts on values and variables to compute a new value; an expression is a legal combination of values and operators, like (my_height+your_height)/2 or area = pi * r * r.

These are the important operators:

Operator Description
Assigns the value on right to the variable on left (e.g., next_year = year + 1)
+ - * /
Arithmetic: add, subtract, multiply, divide
Strings: make a new string by joining the two operands
< <= > >=
Comparisons: less than, less than or equals, greater than, greater than or equals
== !=
More comparisons: equals, doesn't equal
Conditional AND (true if left and right expressions are both true)
Conditional OR (true if either left or right expression is true)

A statement computes a value or does some other operation. For our purposes here, the main kinds of statements are simple function calls like print("...") and input(...), and assignments like x = 1. More complicated statements are described below.

Control Flow Statements

Control flow statements like if-else, while, and for determine what the program will do next, depending on the evaluation of some expression. They are the Python versions of statements like goto and ifzero in the Toy computer, but easier to work with.

The if-else Statement

The if statement is used to select which one of two groups of statements to do next:
if it's raining:
    I will stay home
    I will go outside
or in code:
if condition:

If the condition is true, then the statements are performed, otherwise the other_statements are performed. Both groups of statements can be one or more lines. The statements controlled by if and else must be indented consistently, as shown. Python uses indentation to group statements, and will not compile a program that is not indented properly.

The else part is optional; sometimes the logic doesn't require it and you can omit it. You can also string together a sequence of tests with elif clauses:

temp = int(input("What's the temperature? "))
if temp > 212:
elif temp < 32:
   print("in between")

Make sure you understand how the control flow in this code works. How would you modify it to work with Celsius temperatures?

int() and str()

The result that comes back from input is a Python string, that is, a sequence of characters. Even if that is a sequence of digits, it's not treated as a numeric value; it's just a sequence.

That means that if we want to do arithmetic on it, or compare it to other numeric values, we have to convert it into an integer. The conversion is done with the int function, which converts a string of ASCII digits into the internal representation as a binary integer. (We could also use float.)

Try something like this:

s = "123"
n = int(s)
If you get complaints about strings and integers when you run your Python code, it's likely because you didn't handle the types correcty.


Comments provide a way to add explanatory text to your code, making it more understandable to human readers. In Python, comments are indicated by #. Everything on the line after the # will be ignored by the compiler.

cummings = "e e cummings"  # his preferred spelling
Cummings = "E E Cummings"  # upper-case alternative

The while Loop

The while statement is a loop that causes a group of statements to be repeated while some condition is true:
while my room is a mess: pick up junk, throw it out
In Python, this would look like
while condition:

As long as the condition is true, then the statements will be repeated. (That is, the condition is tested, and if it is true, then the entire loop body is executed, after which the condition is evaluated again.) As with if-else, the statements of the loops are indented.

So, for example, you might print a sequence of temperature conversions like this:

fahr = 0;
while fahr <= 212:
   celsius = 5 / 9 * (fahr - 32);
   print(fahr, "F is ", celsius, "C")
   fahr = fahr + 10
When you use while, watch out for infinite loops. Make sure that you increment or decrement the loop variable, or in some other way ensure that the loop ends eventually.


A function is a chunk of program that can be used or called from other places in a program to do an operation or compute a value; it's a way to wrap up a computation in a package so it can be easily used just by naming it. The Python library includes functions that have already been written for you -- mathematical functions like square root (which is called sqrt in Python) are good examples -- and you can write your own too.

For example, you could write a function to do the Fahrenheit to Celsius temperature conversion; putting the computation in a function makes it easier to change if necessary, and reduces clutter and confusion in the rest of the program. Here is such a function, called ftoc:

def ftoc(fahr):  # convert Fahrenheit to Celsius
   return 5 / 9 * (fahr - 32)
The return statement transfers control back to the function's caller, and sends a value back too if there is one. Notice that the statements of the function are indented.

Functions usually have parameters, so that they can operate on different data values, just like print and input do. Thus ftoc has a single parameter, the temperature to be converted, which will be referred to as fahr within the function.

Now you could use ftoc in a loop like this:

def show_temps(low, high, step):
   f = low
   while f <= high:
      print(f, "F is ", ftoc(f), "C")
      f = f + step
and then call the function in a statement like this:
show_temps(0, 212, 10);
Variables created within a function are only accessible within that function; variables created outside of any function are accessible anywhere in the program.

You won't need to write any functions in this lab, but you will be using functions that others have written for you, so it's important to understand the idea.

Part 3: Writing your own Python program

Your assignment is to implement a program that will play the old guessing game "I'm thinking of a number between 1 and 100". A no-frills version is only about a dozen lines of code. If yours is getting much bigger than that, you're probably off on the wrong track and should review the notes and the Python examples.

Please pay attention to syntax and grammar and language constructs. You can't just throw code fragments around randomly and expect them to work. Read the instructions. All the information you need is right here.

Your program should do the following:

  • When the game starts, it generates a new secret number, then prompts the user to enter a guess.
  • When the user enters a new guess, the program displays the appropriate message (like "50 is too high", "25 is too low", "37 is right"), using print.
  • If the answer was wrong, the user is prompted for another guess.
  • If the answer was right, that's the end of the game.
  • It's a nicer user interface if you combine the appropriate too high/too low message with the prompt for the next guess, but it's not required.

    Writing the program

    Now you can work on playing a single game properly. The sequence of events is
    Generating a secret number

    You will need a variable to hold the secret random number that the user is to guess. Call it secret. At the beginning of your program, you will need to compute a new secret number. Copy these two lines into your Python code.

    import random
    secret = random.randint(1, 100)
    The function random.randint produces a different (pseudo-)random number between 1 and (in this case) 100 each time it is called.

    Temporarily add a print statement after this line to display the secret number, then test this part of the program. (It's easier to test your program if you already know the right answer! Comment out this line of code once everything is working, but leave it in the code.)

    Fetching the Guess
    The next step is to fetch the guess that your user makes. This is the value that comes back from calling input. Store it in a variable called guess. Your code should just ask again for a guess if the input is empty.
    Evaluating the guess

    Now you can go on to the real logic of the program: is the user's guess too high, too low, or just right? Your task is to do this test and decide what message to display and what to do subsequently. This will require a while loop that continues while the guess is not equal to the secret value, an if-else sequence inside the loop that compares the guess to the secret number, and another input() to fetch the next guess. Look back at the discussion of control flow and the examples in the class notes if you need to. Re-read this paragraph.

    One Step at a Time

    If this is your first Python program you are liable to make mistakes. Finding the bugs (debugging) will not be too hard if, as we suggested above, you write a small section at a time, and test as you go. This approach makes it easier to determine what section of your code has the error: it's probably the one you just added.

    Use print statements to display results as your computation proceeds, or to verify that your program is doing what you think it is. These are easy to add and help narrow down problems quickly.

    Inside your program, literal text values must appear inside quotes (e.g., "What's your guess?"). If the Python compiler complains about comparing strings with integers, use the int function to converts a sequence of ASCII digits into their integer value.


    Comments are annotations added by programmers but ignored by Python when it is compiling and running your program. Comments are used to make the code more readable for other people that may have to work on it, or to remind you of what you were thinking when you wrote a particular line of code. Python comments start with # and extend until the end of a line. We have included comments in some of the code above; you are advised to include comments of your own to remind yourself and others of what you are doing.

    When you prepare your lab for submission, you should leave any debugging statements in the program source code, but "commented out" so they can be easily restored if something goes wrong.


    Part 4: Finishing up

    Make sure that you have implemented what we asked for, listed above. If you have time, you could add a line or two to offer the user an upper limit different from 100, or a couple of lines to count the number of guesses, but this is optional.

    If you used Colab, copy your Python code into a text file called

    Upload to the CS tigerfile for Lab 4 at