CS 30 Homework 1

Due by class time Wednesday, February 1

Practice Exercises (Optional)

The exercises below will give you extra practice with Python, but you do not need to turn them in. However, if you have never programmed before, I would strongly recommend doing them.

  1. Read through this online tutorial to get started using IDLE. Note: this tutorial is a little out of date, but most of the information is still valid. You can start IDLE on the Pomona machines by simply typing idle at the Linux prompt.

  2. Exercises 1-5 on pages 22-23 of the Python textbook.

  3. Exercise 4 on page 48. Try these out interactively in IDLE at the Python prompt. Make sure to type each one exactly as shown, and make sure you understand the resulting behavior.

  4. Exercises 1-4 on pages 71-72. Try these out interactively in Python, typing them in exactly as shown.

Problems to Turn In

The following problems should be turned in. Most of them are from your first lab. Using IDLE, create a new file named assign1.py and put all of your program definitions in this file. That way, you can load them all into Python at once for testing by simply pressing the [F5] key.

  1. Problem 8 on page 49: temperature conversion. Call your program farenheit() instead of main(). Hint: the formula for converting Farenheit to Celsius is C = 5 (F - 32) / 9. Here is a sample Python session showing the kind of output your program should produce. Input typed by the user is shown in boldface.

    >>> farenheit()
    Enter a temperature in Farenheit: 212
    212 degrees Farenheit = 100.0 degrees Celsius
    >>> farenheit()
    Enter a temperature in Farenheit: 0
    0 degrees Farenheit = -17.7777777778 degrees Celsius
    >>> farenheit()
    Enter a temperature in Farenheit: 32
    32 degrees Farenheit = 0.0 degrees Celsius
    >>> farenheit()
    Enter a temperature in Farenheit: 72
    72 degrees Farenheit = 22.2222222222 degrees Celsius
    >>>
    
  2. Assuming that 1 minute of time is worth $25 (surely a gross understimate!), write a program to convert time into money. Your program should ask the user for the number of minutes to convert, and then print out a message giving the equivalent dollar amount. Be sure to test your program on several different input values. Call your program time2money().

  3. Problem 1 on page 72: volume and surface of a sphere. Call your program sphere(). The constant pi is available as math.pi (no parentheses) in Python's math library. To use the math library, put the command import math at the top of your file.

  4. Problem 2 on page 72: pizza cost. Call your program pizza().

  5. Problem 9 on page 73: area of a triangle. Call your program triangle().

  6. Problem 11 on page 74: sum of first n natural numbers. Call your program sumFirst().

  7. Problem 12 on page 74: sum of cubes. Call your program sumCubes().

  8. Problem 13 on page 74: sum of numbers entered by user. Call your program sumUser(). Hint: Use an extra variable called total, initialized to 0, to keep track of the running sum. The structure of your program will be similar to the previous ones.

  9. Problem 14 on page 74: average. Call your program average().

  10. Problem 16 on page 74: Fibonacci numbers. Call your program fib().

Turning in Your Homework