IT 116: Introduction to Scripting
Answers to Mid-term Questions

Each of the following questions are worth 4 points.

  1. What is the result of the following calculation in Python's interactive mode?
    7 // 2
    3
  2. Write the Python boolean literals.
    True and False
  3. Write a Python statement that evaluates to True if the variable whose name is num has a value greater than or equal to 0 and less than or equal to 10.
    num >= 0  and num <= 10
  4. When does a while loop stop?
    when the  boolean expression after the keyword while becomes false
  5. What is the name of the Python function used to create a list of numbers in a for loop?
    range
  6. If the function mentioned above were run with the single argument 5, what is the list of numbers that would be created?
    0, 1, 2, 3, 4
  7. If the function mentioned above were run with the two arguments 1 and 5, what is the list of numbers that would be created?
    1, 2, 3, 4
  8. If the function mentioned above were run with the three arguments 1, 10 and 2, what is the list of numbers that would be created?
    1, 3, 5, 7, 9
  9. If the function above is run with only one argument, what does that argument specify?
    one more than the last number in the list
  10. What is the augmented assignment operator that multiples the value on it's right by the value of the variable on it's left and assigns that new value to the variable?
    *=
  11. Write a function header for function named max that takes two integers as parameters.
    def  max(num1, num2):
  12. What do you call a variable that is defined inside a function?
    a local variable
  13. If the variable is defined inside a function, can the code outside all functions use that variable?
    no
  14. What do you call a variable that is defined outside any function?
    a global variable
  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. Define the function positive_negative_zero which takes one integer as a parameter.
    The function must print "Positive" if the argument is greater than zero, "Negative" if the argument is less than zero, and "Zero" if the argument is zero.
    def positive_negative_zero(number):
        if number > 0:
            print("Positive")
        elif number < 0:
            print("Negative")
        else:
            print("Zero")
  2. Define the function odd_numbers_for_loop which takes one integer as a parameter.
    The function must use a for loop to print the odd numbers from 1 up to and including the parameter.
    def odd_numbers_for_loop(limit):
        for number in range(1, limit + 1, 2):
            print(number)
  3. Define the function count_down which takes one integer as an parameter.
    The function must use a while loop to print the numbers, starting with the value of the parameter down to 0.
    def count_down(num):
        while num >= 0:
            print(num)
            num -= 1
  4. Define the function print_integer which takes two parameters, min and max. This function asks the user for an integer between min and max.
    If the number entered is not in the proper range, the function prints an error message and asks again for an integer.
    When the function gets an integer in the proper range, it prints that number.
    def print_integer(min, max):
        num = int(input("Please enter an integer between " + str(min) + " and " + str(max) + ": "))
        while num <= min or num >= max:
            print("The number was not between "  + str(min) + " and " + str(max))
            num = int(input("Please enter an integer between " + str(min) + " and " + str(max) + ": "))
        print(num)