Quiz 2 Study Guide

You are responsible for all material needed for Lab 3.

We will not test you on functions this week other than main.

Study guides are not graded. You may work with other students on the questions, and ask questions about the guide during evening ninja help sessions, or with meetings with faculty and staff. We do not provide full solutions to the study guides.

The study guide is not indicative of length, difficulty, or format of the quiz.

In addition to all concepts from Quiz 1, you should understand the following:

Python concepts and functions

You should be able to use the following Python concepts and functions:

  • the bool data type representing True and False

  • if, if/else, if/elif/else

  • the relational operators <, <=, ==, >, >=, !=

  • how strings are ordered when compared using relational operators.

  • the logical operators and, or, and not

  • nested statements (e.g., using an if/else in a loop)

  • while loops

Sample practice problems

  1. Evaluate the following python expressions.

    Standards: Evaluate python expressions to determine their values and types

    Given these assignments:

    x = 5
    y = 13
    phrase = "computer science"
    name = "Kevin"
    b = False

    Show the value and type of each expression below:

    (x < 10) and (y < 10)
    (x < 10) or (y < 10)
    (x < 10) and (x > 0)
    (x > 10) or (x < 0)
    (5/x) > 7.0
    not b
    b or (not b)
    len(phrase) >= 10
    phrase[0] < phrase[1]
    name < phrase
  2. The code below asks the user for two input values. Provide 5 different sets of input values for first and second such that each branch of the if statement is followed.

    Standards: If statements

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    def main():
        first = int(input("Enter the first number: "))
        second = int(input("Enter the second number: "))
        if (first < 10):
            print("Option 1")
        elif (second < 10):
            print("Option 2")
        elif (first < second):
            print("Option 3")
        elif (second > 20):
            print("Option 4")
        else:
            print("Option 5")
    
    main()
    
  3. Show the output from the following code fragments:

    Standards: The accumulator pattern, For loops

    word = 'february'
    result = ''
    for letter in word:
        result = result + letter
    print(result)
    word = 'february'
    result = ''
    for i in range(len(word)):
        result = result + word[i]
    print(result)
    1. Modify the first block of code so that it prints yraurbef (spelled backwards) instead of february.

    2. Modify the second block of code so that it prints yraurbef (spelled backwards) instead of february.

    3. There are multiple ways you can modify the second block of code to do this. Can you think of another way to modify the second block to print yraurbef?

    4. Modify the second block of code so that it prints fbur (every other letter is skipped).

  4. Write a program that asks the user to enter a word and a letter.

    Standards: The accumulator pattern, For loops, If statments, Input / Output

    Your program will then count how many times that letter appeared in the word. For example:

    Enter a word: mississippi
    Enter a letter: s
    The letter s appears 4 times in mississippi
  5. Write a program that asks the user to enter the hour of the day and whether it is AM or PM. You can assume the user always provides an integer between 1-12 for the hour and that they always type either AM or PM. Based on the time of day, tell them if Narples is open and what meal they are serving. For this question, assume that Narples serves breakfast from 7 AM to 10 AM, lunch from 11 AM to 2 PM, dinner from 4 PM to 9 PM, and is closed every other time.

    Standards: If statments, Input / Output

    For example:

    What is the hour? 8
    Is that AM or PM? AM
    Narples is serving breakfast.
    
    What is the hour? 2
    Is that AM or PM? PM
    Narples is serving lunch.
    
    What is the hour? 9
    Is that AM or PM? PM
    Narples is serving dinner.
    
    What is the hour? 11
    Is that AM or PM? AM
    Narples is serving lunch.
    
    What is the hour? 11
    Is that AM or PM? PM
    Narples is closed.
  6. Show the output from the following code fragment.

    Standards: While loops, The accumulator pattern

    x = 0
    while (x < 6):
          print("%d  %d" % (x, x*x))
          x = x + 1
    print("done!")
  7. Show the output from the following code fragment.

    Standards: While loops, If statements, The accumulator pattern

    text = "strings!"
    i = 0
    found = False
    while (found == False):
        if (text[i] == "i"):
            found = True
        else:
            print(text[i])
            i = i + 1
    if (found == True):
        print("found at position %d" % (i) )
    1. Can you think of a way to modify the code so that it prints "not found" if the letter "i" is not in the string?