IT 116: Introduction to Scripting
Answers to Midterm Questions - Even 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. Write the Python boolean literals.
    True and False
  3. What are the only values that a boolean expression can have?
    True and False
  4. What do you call a value written out directly inside the code?
    a literal
  5. What is an expression?
    anything that can be turned into a value
  6. What do you call a loop that does not stop?
    an infinite loop
  7. What do you call a a sequence of characters?
    a string
  8. What is the result of the following calculation in Python's interactive mode?
    7 // 2
    3
  9. What do you call a value given to a function when it is run?
    an argument
  10. What do you call a statement that runs a function?
    a function call
  11. What do you call the variables that are listed inside the parentheses of a function header?
    parameters
  12. If you want to stop a Python script running on Unix, what do you do?
    Control C
  13. If you typed the following in Python's interactive mode, what would the result be?
    True or False
    True
  14. If you typed the following in the Python interactive mode, what would the result be?
    True and False
    False
  15. What do you call a series of statements that has a name and performs some task?
    a function
The following questions require you to write code.
Each question is worth 10 points.

  1. 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")
  2. 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)
  3. 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: "))
  4. 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")