Introduction to Computer Programming

Lab 3: indefinite loops and multiway branches

pre-lab exercises


  1. Python basics in the IDLE shell.

    Try these examples in the IDLE shell, in order, one at a time:

     >>> n = 47
     >>> n < 50
     >>> n > 100
     >>> n > 50
     >>> n < 100
     >>> n < 100 and n > 50
     >>> n > 50 or n < 100
     >>> not (n > 50)
     >>> ord('Q')
     >>> ord('R')
     >>> 'Q' < 'R'
    
     >>> for x in range(37, 42):
             print(x)
    
     >>> x = 37
     >>> while x < 42:
             print(x)
             x = x + 1
    
     >>> from random import randrange
     >>> randrange(3)
     >>> randrange(3)
     >>> randrange(3)
    
     >>> from time import sleep
     >>> sleep(3)
     >>> sleep(0.25)
    
     >>> j = 0
     >>> while j < 100:
             print(j)
             sleep(0.5)
             j = randrange(50, 150)
    
     >>> j
  2. Download and unzip the pre-lab archive. Use IDLE to open lab3_examples.py in the pre-lab3 folder. Read over the file in detail. Bring questions to ask at the start of lab.

  3. Run the module in IDLE and then experiment with using each of the functions in the IDLE shell, as in:

     >>> is_upper_char('P')
     >>> uppers_only('Computers Only Offer Loops!')
     >>> count_factors(30) 
     >>> steps_to_zero(19)
     >>> smallest_factor(98769876345281112)
     >>> int_to_string(4301)
     >>> random_boxes(8)