Docsity

Homework 1 - Python's First Bite | CS 1301, Assignments of Computer Science

Toggle sidebar
bg1
Homework 1 CS 1301 โ€“ Fall 2006 Page 1/5
CS 1301 โ€“ Fall 2006
Homework 1 - Python's First Bite
Due: Friday, September 1, 2006 at 6 PM EST
Files to submit:
1. sphereVolume.py
2. heronFormula.py
3. tipCalculator.py
For Help:
๎šƒ Newsgroups โ€“ see course syllabus for more detailed information
๎šƒ TA Helpdesk โ€“ schedule will be posted on class website
๎šƒ Email TAs
Notes:
๎šƒ Be sure to complete Lab 1 before attempting this homework
๎šƒ Donโ€™t forget to include the required comments and collaboration statement (as outlined
on the course syllabus)
๎šƒ Be careful when you submit multiple files to WebWork
๎šƒ Retrieve your submission from WebWork and rerun it to be sure it still works okay
๎šƒ Do not wait until the last minute to do this assignment incase you run into problems
๎šƒ If you find an error in the homework assignment, let a TA know immediately
Information you might need:
Built in Python Functions: http://docs.python.org/lib/built-in-funcs.html
raw_input(
[prompt])
If the prompt argument is present, it is written to standard output without a trailing
newline. The function then reads a line from input, converts it to a string (stripping a
trailing newline), and returns that. Example:
>>> s = raw_input('--> ')
--> Monty Python's Flying Circus
>>> s
"Monty Python's Flying Circus"
float(
[x])
Convert a string or a number to floating point. If the argument is a string, it must contain
a possibly signed decimal or floating point number, possibly embedded in whitespace.
Otherwise, the argument may be a plain or long integer or a floating point number, and a
floating point number with the same value (within Python's floating point precision) is
returned. If no argument is given, returns 0.0.
pf3
pf4
pf5

Partial preview of the text

Download Homework 1 - Python's First Bite | CS 1301 and more Assignments Computer Science in PDF only on Docsity!

CS 1301 โ€“ Fall 2006

Homework 1 - Python's First Bite

Due: Friday, September 1, 2006 at 6 PM EST

Files to submit:

**1. sphereVolume.py

  1. heronFormula.py
  2. tipCalculator.py**

For Help: ยƒ Newsgroups โ€“ see course syllabus for more detailed information ยƒ TA Helpdesk โ€“ schedule will be posted on class website ยƒ Email TAs

Notes: ยƒ Be sure to complete Lab 1 before attempting this homework ยƒ Donโ€™t forget to include the required comments and collaboration statement (as outlined on the course syllabus) ยƒ Be careful when you submit multiple files to WebWork ยƒ Retrieve your submission from WebWork and rerun it to be sure it still works okay ยƒ Do not wait until the last minute to do this assignment incase you run into problems ยƒ If you find an error in the homework assignment, let a TA know immediately

Information you might need:

Built in Python Functions: http://docs.python.org/lib/built-in-funcs.html

raw_input ( [ prompt ])

If the prompt argument is present, it is written to standard output without a trailing newline. The function then reads a line from input, converts it to a string (stripping a trailing newline), and returns that. Example: >>> s = raw_input('--> ') --> Monty Python's Flying Circus >>> s "Monty Python's Flying Circus"

float ( [ x ])

Convert a string or a number to floating point. If the argument is a string, it must contain a possibly signed decimal or floating point number, possibly embedded in whitespace. Otherwise, the argument may be a plain or long integer or a floating point number, and a floating point number with the same value (within Python's floating point precision) is returned. If no argument is given, returns 0.0.

str ( [ object ])

Return a string containing a nicely printable representation of an object. For strings, this returns the string itself. The difference with repr( object ) is that str( object ) does not always attempt to return a string that is acceptable to eval(); its goal is to return a printable string. If no argument is given, returns the empty string, ''.

Math Module Functions: http://docs.python.org/lib/module-math.html

sqrt ( x ) Return the square root of x.

Part 1 โ€“ sphereVolume.py

Remember from high school geometry that the volume of a sphere is:

We are going to program a spherical volume calculator in Python.

We can break this problem into 3 steps:

  1. Prompt the user for the radius
  2. Compute the volume
  3. Print out the answer

To prompt the user for input, use raw_input(). You need to convert the answer from a string to a float using the float() function.

For example: temp = raw_input("Please enter a number: ") x = float(temp)

now the variable x holds the float value

To compute the volume, you will need to multiply all of the parts together (the fraction 4/3, pi, and the cubed radius).

Remember that python uses integer division, so 4/3 =1, but 4.0/3.0 = 1.3333333.

Since we want our calculation to be fairly exact, we should use Pythonโ€™s built-in value for pi, not just 3.14. To use the pi constant, you need to import the math module. (it is best to put the import math statement as your first line of code) Once you have imported the module, you can treat the constant math.pi the same as if it were a number.

Use raw_input() to prompt the user 3 times for each of the sides of a triangle. Remember to convert the input into a float.

After you have successfully stored all 3 of the sides as floats, compute and print s. Then compute and print the area using math.sqrt(). Remember that you will need to convert the floats to strings using str() when you print the message.

Please do not round the answer.

If you want to make sure you got it right, try using a familiar triangle such as a Pythagorean triple (6-8-10) (you should be able to easily manually compute the area of a right-triangle) or a 10-10-10 equilateral triangle.

Sample output: >>> What is the length of side a? 6 What is the length of side b? 8 What is the length of side c? 10 s = 12. Area of triangle = 24. >>>

>>> What is the length of side a? 10 What is the length of side b? 10 What is the length of side c? 10 S = 15. Area of triangle = 43. >>>

Part 3 โ€“ tipCalculator.py

By now, you are probably tired of doing geometry problems in a computer science class at 2am, so you go grab something to eat at the only place open at this time of night: Waffle House.

In this tired state, there is no way that you will be able to compute the proper tip amount for the Waffle House ladies in your head, so you write a quick Python program to do it for you.

You program will also need to be able to handle tax.

Write a program that prompts the user for the bill amount and the percent that they want to leave for the tip. Do not include the dollar sign or percent sign when entering the numbers!

Calculate the tax (always 7%), the tip (percent of bill amount, excluding tax), and the total bill.

Remember to divide the percentages by 100 when multiplying.

You are NOT expected to round the answer to the nearest cent - but we encourage you to try if you find the homework very easy. Python has a built-in round() method you may find handy.

Sample output: >>> How much is the bill (before tax and tip)? 10 What percent tip do you want to leave? 15 Tax is $0. Tip is $1. Total is $12. >>> How much is the bill (before tax and tip)? 15. What percent tip do you want to leave? 20 Tax is $1. Tip is $3. Total is $20. >>>

Submitting to WebWork

You learned how to submit a file to webwork in Lab 1, but now you need to submit multiple files. This is accomplished by simply adding more files once you add the first one. The box that lists the files you're uploading should show all three files before you click submit. Also, one important note is that while you can resubmit homeworks, it COMPLETELY erases the previous submission. So, suppose you submitted sphereVolume.py, heronFormula.py, and tipCalculator.py the first time, but then go and change something in tipCalculator.py You will need to resubmit ALL your files, if you only resubmit tipCalculator.py, the other 2 files will be erased. Remember to submit -all- your files when resubmitting, even if you haven't modified them.

Also, be certain you name your files -exactly- as indicated. SPHEREVOLUME.PY is not the same as sphereVolume.py, and if the autograder can't find your file because it was misnamed, then you're out of luck for that program. This is not to punish you, but merely so we can get your grades out faster.

Written by Joshua Silver, Fall 2006