To Lecture Notes

IT 211 -- Feb 13, 2019

Review Exercises

  1. What does += mean?
    Ans: Is an assignment operator, which is used to update a variable by adding the value on the right to the variable on the left. For example, the expression
    number_of_customers += 1
    
    adds one to the variable on the left. This means the same thing as
    number_of_customers = number_of_customers + 1
    
  2. Combine the these two lines into one line:
    fields = line.strip( ).split(',')
    salary = int(fields[4])
    
    Ans:
    salary = int(line.strip( ).split(',')[4])
    # This is an example of "macho programming."
    
  3. Find the errors in this script. It is supposed to list the age, city, and salary of an employee whose name is entered at the keyboard. The input file is employees.txt. There are about 13 errors.
    desired_name = input("Enter desired name: ")
    fin = ("employees.txt", r)
    line = fin.readline( )
    while line != ""
        fields = line.split (',')
        name = fields[0].strip( )
        age = fields[2].strip( )
        city = fields[3].strip( )
        salary = fields[4].strip( )
        if name = desired_name:
            print("Age: {age}")
            print('City: {city}')
        print(f"Salary: # {salary}")
        close(fin)
    
  4. What is the output?
    for y in range(0, 5):
        for x in range(0, 2 * x):
            print("#", end="")
        print( )
    

More about User Defined Methods