CMPT 120 Lab 3

This site applies only to CMPT 120 D1 (Burnaby) in Summer 2011. See the other instructors' pages for other sections.

  1. Create a text file or answer this question on paper. Indicate the result of these boolean expressions. Try to figure out the answer for each before you check it in the interpreter. Assume that these variables have first been set as follows:
    alpha = 120
    beta = 225
    gamma = 120.0
    delta = "120"
    a = "hello"
    b = "goodbye"
    c = "good riddance"
    bool = 3==4

    Now, how do these evaluate? (Each answer should be True or False.)

    1. alpha == 225
    2. alpha != beta
    3. alpha == delta
    4. a == "hello"
    5. a < b
    6. 120==alpha and beta==120
    7. 120==alpha or beta==120
    8. not bool
    9. bool and 120!=225
    10. bool or 120!=225
  2. By using an if-elif-else structure, write a program that reads a number entered by the user. The entered number should be in the range 0–100. (Assume that the user enters a floating point number.)

    The number entered by the user should then be checked to make sure it's in the right range. If a user enters a number outside of the range 0–100, then your program should display the following message and exit:

    Sorry, num is out of range.

    [Replacing num with the value the user entered.]

    If they entered a number in the range, the appropriate message should be displayed:

    RangeMessage
    90–100num is excellent!
    80–90num is very good.
    65–80num is average.
    50–65num needs improvement.
    0–50num is not so good.

    For each number that is on a boundary (90.0, 80.0, 65.0, 50.0), the number should go with the range below. (So, 90 is very good, not excellent.)

    Here are four separate sample runs of the program:

    Enter your score: 82.2
    82.2 is very good.
    Enter your score: 68.123
    68.123 is average.
    Enter your score: 65
    65.0 needs improvement.
    Enter your score: 0
    0.0 is not so good.
    Enter your score: 103.2
    Sorry, 103.2 is out of range.
  3. Create a Python program that asks the user for a number (integer). The program should then tell the user how many hundreds can go into the number, and how much is left over.

    For example,

    Enter a number: 9932
    9932 = 99*100 + 32
    Enter a number: 89
    89 = 0*100 + 89

    If the number is negative, it should indicate the number of hundreds, subtracting the remainder:

    Enter a number: -836
    -836 = -8*100 - 36
    Enter a number: -34234
    -34234 = -342*100 - 34
    Enter a number: -72
    -72 = 0*100 - 72

    Hint: the % operator calculates the remainder of a division. For example, 10 % 3 gives a result 1.

    Hint: Deal with the positive and negative values in separate parts of an if-else structure. Get the calculation for positive values working first. For negative values, make them positive (num = -num) and then use the same method as for positive values, adapting as appropriate.