To Lecture Notes

IT 211 -- Jan 23, 2019

Review Exercises

  1. What is the Python symbol for line continuation?
    Ans: Backslash. For example:
    sum = x1 + x2 + x3 + \
          x4 + x5 + x6
    
    No spaces or tabs are allowed on the line after the backslash.
  2. Construct the variable trace and predict the output for these examples:

         Trace3    Trace4

    Check your answer by running the script in each case.
  3. Rewrite the last line of this script using an F-string:
    person1 = "Bill"
    person2 = "Dan"
    amt = 300.0
    print(person1, " owes ", person2, " $", str(amt), ".")
    
    Ans:
    print(f"{person1} owes {person2} ${amt}.")
    
  4. What is printed when the int values 2 and 3 are input from the keyboard? Construct the variable trace to help you predict the output.
    x = int(input("Enter x value: "))            
    y = int(input("Enter y value: "))                    
    x = y                              
    y = x                              
    print(f"{x} {y}") 
    
    Ans:
    Variable Trace:   x     y
                    -----+-----
                      2     3
                      3     3
    Output:
    3 3
    When you execute x = y, you lose the value of 
    2 in x.
    
  5. Improve the script in Exercise 4 to successfully swap the values of x and y?
    x = int(input("Enter x value: "))            
    y = int(input("Enter y value: "))                    
    temp = y
    y = x
    x = temp
    print(f"{x} {y}")
    
    # Ans:
    # Variable Trace:  Output: 3 2
      x     y   temp
    -----+-----+-----
      2     3     3
      2     2     3
      3     2     3
    
    You could also use this special Python command x,y = y,x that assigns x and y new values at the same time:
    x = int(input("Enter x value: "))            
    y = int(input("Enter y value: "))                    
    y, x = x, y
    print(f"{x} {y}")
    # Output: 3 2
    
  6. Find the errors in this script:
    // TipCalculator Script
    amt = input("Enter amount of check: ')
    tip = amt + percent
    percent = input(f"Enter tip percentage: "
    if tip > 1.00
        tip = amt + percentage
    elif:
        tip = 1.00
    
    print(f"Tip: ${tip}.")
    
    # Corrected Script:
    amt = float(input("Enter amount of check: "))
    percent = float(input("Enter tip percentage: "))
    tip = amt * percent / 100
    if tip < 1.00:
        tip = 1.00
    
    print(f"Tip: ${tip}.")
    
  7. An infinite loop is a loop that never terminates, for example:
    while True:
       print("I am in an infinite loop.")
    
    Why are each of these infinite loops?
    # Loop 1:
    n = 0
    while n <= 100:
        print(n)
    # Ans: There is no n = n + 1 statement to change
    # the value of n.
    
    # Loop 2:
    while n <= 100:
        n = 0
        print(n)
        n = n + 1
    # Ans: the initialization n = 0 is inside the loop, so the
    value of n is reset to 0 each time the loop executes.
    
    # Loop 3:
    n = 0
    while n <= 100:
        print(n)
    n = n + 1
    Ans: The iteration statement n = n + 1 is not indented, so it is not
    in the while loop.  Therefore n never changes.
    
  8. Translate these lines from the pseudocode for Project 2b:
    set human score to 0.0.
    set computer score to 0.0.
    
    while true
    
        input human play with prompt.
        choose random number from 0, 1, or 2.
    
        if random number is 0
            Set computer play to rock.
        else if random number is 1
            Set computer play to paper.
        else
            Set computer play to scissors.
        end
    
        if human plays rock and computer plays paper
            Output paper covers scissors, computer wins.
            Add 1.0 to computer's score.
        end
    
    end
    
    # Python translation:
    human_score = 0
    computer_score = 0
    
    while True:
    
        human_play = input("Enter human play: ")
        random_number = random.randrange(0, 3)
    
        if random_number == 0:
            computer_play = "rock"
        elif random_number == 1:
            computer_play = "paper"
        else:
            computer_play = "scissors"
    
        if human_play == "rock" and computer_play == "paper":
            print("Rock covers paper")
            print("Computer wins.")
            computer_score = computer_score + 1
       
    

Finish Discussing Project 2b

Definite Loops