To Lecture Notes

IT 211 -- Feb 11, 2019

Review Exercises

  1. What does it mean to say that a computer can only add two numbers at a time?
    Ans: Even if a Python statement adds three numbers like this:
    n = 1 + 2 + 3
    
    the addition proceeds from left to right, the first two numbers are added; then the sum is added to the third: 1 + 2 == 3; 3 + 3 == 6. Even if 100 numbers are added in this loop:
    sum = 0.0
    for n in 1..100
      sum += n
    end
    print sum
    
    n is added to sum to produce the updated sum.
  2. Translate this pseudocode into Python. This pseudocode is supposed to compute the product of a list of numbers, terminated by a blank line.
    initialize product
    input a line
    while line is not blank
        convert line to float value
        update product
        input another line
    end
    output product
    
    Ans:
    product = 1.0
    line = input("Enter a float value: ")
    while line != "":
        value = float(line)
        product *= value
        line = input("Enter a float value: ")
        
    print("Product: ", product)
    
  3. Convert your script from Exercise 4 to a script that reads the floating point values from the input file values.txt.
    Ans: 
    product = 1.0
    fin = open("values.txt", "r")
    line = readline( )
    while line != "":
        value = float(line.strip( ))
        product *= value
        line = readline( )
        
    print("Product: ", product)
    

Sequential Processing Practice Problems

Fix up the the Employees Example to work these problems. Use the input file employees.txt.
Here are the answers to the following sequential processing problems.

  1. What is the average salary of all employees?
  2. The average salary for the women.
  3. The average age of the male employees in Chicago.
  4. The average salary of the desired city, where the desired city is entered from the keyboard.

User Defined Methods

Nested for Loops

Project 4