IT 116: Introduction to Scripting
Answers to Midterm Questions - Odd Columns

Each of the following questions is worth 4 points.

  1. What do you call one or more lines of code that perform a complete action?
    a statement
  2. What do you call a value given to a function when it is run?
    an argument
  3. What is an expression?
    anything that can be turned into a value
  4. What are the four types of expressions?
    literals, variables, calculations, function calls
  5. What is the result of the following calculation in Python's interactive mode?
    7 // 2
    3
  6. Write the Python boolean literals.
    True and False
  7. If you typed the following in Python's interactive mode, what would the result be?
    not False
    True
  8. If you typed the following in Python's interactive mode, what would the result be?
    True and False
    False
  9. If you typed the following in Python's interactive mode, what would the result be?
    True or False
    True
  10. If you typed the following in Python's interactive mode, what would the result be?
    not True
    False
  11. What happens in a while loop when the boolean expression after the keyword while becomes false?
    the loop stops
  12. What is the augmented assignment operator that subtracts the value on its right from the value of the variable on its left and assigns that new value to the variable?
    -=
  13. What do you call a variable that is defined inside a function?
    a local variable
  14. What do you call the variables that are listed inside the parentheses of a function header?
    parameters
  15. Where do parameters get their values?
    from the corresponding argument in the function call
The following questions require you to write code.
Each question is worth 10 points.

  1. Write an if-elif-else statement that behaves like a traffic light.
    This statement should test the value of the variable named signal.
    If signal is "green" it should print "Go".
    If signal is "yellow" it should print "Slow".
    If signal is "red" it should print "Stop".
    Otherwise it should print "Error".
    You should assume that signal already has a value.
    if signal == "green":
        print("Go")
    elif signal == "yellow":
        print("Slow")
    elif signal == "red":
        print("Stop")
    else:
        print("Error")
  2. Write a while loop that asks the user for a number greater than 10.
    If the user enters a number that is not greater than 10, the loop should print an error message,
    then ask again for a number greater than 10.
    If the number entered is greater than 10, it should exit the loop and print the number.
    num = int(input("Number greater than 10: "))
    while num <= 10:
        print(num, "is not greater than 10")
        num = int(input("Number greater than 10: "))
    print(num)
  3. Write a for loop that prints the even numbers from 2 to 10 using the range function.
    for num in range(2,11,2):
    print(num)
  4. Define the function print_cubes, that takes two parameters, min and max, both integers.
    The function should print the cubes of the numbers between min and max.
    The cube of a number is the number raised to the 3rd power.
    The function must use a for loop with the range function.
    def print_cubes(min, max):
    for n in range(min, max +1):
        print(n * n * n