Loops Exercises

  1. Write a python script that requests a starting number and a stopping number from the user. Display each number from the starting number to the stopping number on its own line. For example:
    Please enter a number: 5
    Please enter another number: 9
    5
    6
    7
    8
    9
    
    Another example:
    Please enter a number: 9
    Please enter another number: 5
    5
    6
    7
    8
    9
    
  2. Write a python script that requests a starting number and a stopping number from the user. Display the sum of all numbers from the starting number to the stopping number (inclusively). For example:
    Please enter a number: 5
    Please enter another number: 9
    The sum of the numbers between 5 and 9 is 35
    
    Another example:
    Please enter a number: 9
    Please enter another number: 5
    The sum of the numbers between 5 and 9 is 35
    
  3. Name Game Demo:
    Please type your name:
    
    1. Draw a flowchart of the algorithm that the game used
    2. Write python code for the name game
  4. Write python code to request either "yes" or "no" from the user. Continue to prompt the user until either "yes" or "no" is entered. Then display their response. Examples:
    Please enter yes or no: maybe
    Please enter yes or no: Yes
    Please enter yes or no: yes
    Thank you for entering yes
    Please enter yes or no: why
    Please enter yes or no: why
    Please enter yes or no: why
    Please enter yes or no: no
    Thank you for entering no
    Please enter yes or no: yes
    Thank you for entering yes
  5. Write python code using a while statement to produce the exact same results as the code below:
    # set the initial number
    startNum = 10
    
    # count down until we get to 1
    for num in range(startNum, 0, -1):
        print( num, "...")
    print( num, "!", sep='')
    
  6. What is the output of the following script:
    print( "Before the for loop ..." )
    for i in range( 100, 0 ):
        print( i )
    print( "All done" )
    
  7. What is the output of the following script:
    pigsFly = True
    print( "Before the while loop ..." )
    while pigsFly == False:
        print("I'm not going to change")
    print("Ok, I'll change")
  8. What is the output of the following script:
    pigsFly = False
    print( "Before the while loop ..." )
    while pigsFly == False:
        print("I'm not going to change")
    print("Ok, I'll change")
    
  9. Below is a python script that will display a table (a TAB-delimited output) of sin() values (for every 30 degrees):
    import math
    
    print("Radians" + "\t" + "sin()")
    for degrees in range(0, 361, 30):
        rads = math.radians(degrees)
        sinValue = math.sin(rads)
        print( round(rads,3), round(sinValue,3), sep='\t')
    
    Modify the script to display a table that includes degrees in the first column. For example:
    Degrees	Radians	sin()
    0	0.000	0.000
    30	0.524	0.500
    60	1.047	0.866
    90	1.571	1.000
    120	2.094	0.866
    150	2.618	0.500
    180	3.142	0.000
    210	3.665	-0.500
    240	4.189	-0.866
    270	4.712	-1.000
    300	5.236	-0.866
    330	5.760	-0.500
    360	6.283	-0.000
    Now, add a column for cos().
  10. What does the following code display?
    MAX_NUM = 15
    for i in range(1, MAX_NUM + 1):
        for j in range(1, MAX_NUM + 1):
            print(i*j, end='\t')
        print()
    
  11. What does the following code display?
    BASE = 10
    
    for j in range( BASE ):
        for k in range( BASE ):
            print(j, k, sep="")