Lab 2: if and while

Comp 150, Dordal, Feb 3, 2006 (birthday of Felix Mendelssohn).

In this lab we will write two simple functions, one to print a verbal description of the weather (more specifically, the temperature), and one to rewrite the fact function from the previous lab using a while loop.

If you have not finished the previous lab, now would be a good opportunity to do that. Most of you finished the quad function last Friday. The function f(n) = n/fact(n)**(1.0/n) should be very quick to implement. The boldify function is a few lines longer, but it is important that you figure out how to get it working. Ask me or Debjit if you are having trouble with either of these.

Again, to start python: create a command window, change directories to where your files will go, and type "python". Start up your preferred editor separately, navigate to the same directory, and edit your files. The NotePad editor is popular for this; I like textpad.

I did learn one useful thing this week. If you import the file "lab2.py" using

    import lab2
then to run your functions you have to include "lab2." in the name: lab2.fact2(10). This is easy to forget. But if you import as follows:
    from lab2 import *
instead of the previous way, then you don't need the "lab2." prefix: you can just run fact2(10).

Use reload(lab2) the usual way.

The functions

Your actual assignment is to complete the following two functions and email the lab2.py file to me (pld@cs.luc.edu).

1. Temperature

On page 202 of Zelle is a function that converts from Fahrenheit to Celsius. What I want is to convert from Fahrenheit (or Celsius, your choice, but be sure to make your choice clear to me) to words (that is, strings):

You are to do this using a big if statement, as illustrated on page 212 of Zelle. The example on page 202 does print hot/cold descriptions, but it uses two separate if statements and is hard to extend. Try to do it all in one big ; note that elif is a special shorthand for else if; if you try to use else if, though, you have to keep indenting more and more which becomes unreadable.
if temp >=90:
    print "too darn hot"
elif temp >= 70:
    print "just right"
elif temp >= 40:
    print ...
else:
    print "cold as this program gets"
Note that in the else case, above, occurs when temp < 40, and that in all cases only one option is printed.

Try to cover at least four temperature ranges (two elif's).

2. Factorial loop

Here is a definition of the factorial function using a for loop:

def fact(n):
    prod = 1
    for factor in range(1,n):
        prod = prod * factor
    return prod
Recall that the for loop runs the variable factor through each member of the list [1,2,3....,n-1], which is created by the range(1,n) call. While the creation of simple lists is pretty fast, it is fundamentally unnecessary here and in some cases can degrade performance.

You are to rewrite this using a while loop; that is, without using for or range.

Here are a few examples. Do not forget the i=i+1 increment!

To find the sum of the numbers from 1 to N:

	N = 100
	i= 1
	sum = 0
	while i<=N:
	    sum = sum + i
	    i = i+1
The value of 1+2+...+N is now in variable sum.

To find the sum of the numbers in a list L:

    L=[2,3,5,7,11,13,17,19]
    i=0
    sum = 0
    while i<len(L):
        sum = sum + L[i]
        i = i+1

To reverse a string S. The reversed string will be in the variable revS, at the end of the loop.

    S = "hello, world"
    i = len(S)-1;		# last position of S
    revS = ""
    while i>=0:
        revS = revS + S[i]
        i = i-1			# this time i is DECREMENTED