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 series of statements that has a name and performs some task?
    a function
  3. What do you call a value given to a function when it is run?
    an argument
  4. What do you call a a sequence of characters?
    a string
  5. What do you call a value written out directly inside the code?
    a literal
  6. What do you call a statement that runs a function?
    a function call
  7. What is an expression?
    anything that can be turned into a value
  8. What is the result of the following calculation in Python's interactive mode?
    7 // 2
    3
  9. What are the only values that a boolean expression can have?
    True and False
  10. Write the Python boolean literals.
    True and False
  11. If you typed the following in the Python interactive mode, what would the result be?
    True and False
    False
  12. If you typed the following in Python's interactive mode, what would the result be?
    True or False
    True
  13. What do you call a loop that does not stop?
    an infinite loop
  14. If you want to stop a Python script running on Unix, what do you do?
    Control C
  15. What do you call the variables that are listed inside the parentheses of a function header?
    parameters
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 an even number.
    If the user enters an odd number, it should print an error message and ask for another number.
    You will need the remainder operator, % to perform this test.
    You should assume that that the code already has an integer value stored in the variable named num.
    while num % 2 == 1:
        print(num, "is not an even number")
        num = int(input("Even number: "))
  3. Write a for loop that prints the odd numbers from 3 to 11.
    You MUST use the range function with 3 arguments.
    for num in range(3, 12, 2):
        print(num)
  4. Define the function odd_even_print.
    This function has one parameter num, an integer.
    The function must print "Even" if the number is even and "Odd" if the number is odd.
    You will need to use the remainder operator %.
    def odd_even_print(num):
        if num % 2 == 0:
            print("Even")
        else:
            print("Odd")