CMPT 120 Lab 2

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. The following are attempts at writing a Python statement. For each one, is it valid (i.e. will it execute with no errors)? If there is an error, why? Try to figure out the answer for each before you check it in the interpreter.

    Assume that the interpreter has just been started (no variables are defined) and we attempt these “statements” in this order.

    1. one hundred = 100
    2. pi = 3.1994
    3. 3.1416 = pi
    4. seven = six + 1
    5. print 100/2/5
    6. print '100/2/5'
    7. print x=10
    8. print "Hello, world
  2. Create a Python program named circle.py that calculate the area of a circle for the user. Here are two examples of what the program should look like when you run it:
    Enter the radius of the circle: 10
    The area of the circle is 314.16.
    Enter the radius of the circle: 39.43
    The area of the circle is 4884.32374584.

    Use the formula 3.1416r2 to calculate the area.

  3. Copy your program circle.py to circle2.py. In circle2.py, modify the program so it will refuse to run if the user enters a radius that is less than zero:
    Enter the radius of the circle: -10
    Sorry, I can't do a negative radius.
    Enter the radius of the circle: 39.43
    The area of the circle is 4884.32374584.

    It should still do the same calculation for values of zero or more. Hint:

    if radius < 0:
  4. In a text file or on paper, convert each of these unsigned binary values to decimal integers. Show the calculation you did to get the decimal value.
    1. 00110011
    2. 11111111
    3. 01111111
    4. 10010110
  5. Repeat the previous question, treating the values as signed (two's complement) integers.