Professional Documents
Culture Documents
Homework 1
Homework 1
Homework 1
● The area of a circle with radius r is πr2. With π = 3.14, what is the area of a circle
with radius 5?
● Suppose that the cover price of a book is $24.95, but bookstores get a 40% discount.
Shipping costs $3 for the first copy and 75¢ for each additional copy. What is the
total wholesale cost for 60 copies?
Upon completing each exercise, save the output from your interactive session as ex2.txt
3. Tax, tip and total
Write a Python script to calculate the total amount of a meal purchased at a restaurant. It should
ask the user to enter the charge for the food and then calculate and output the amounts of 6.35%
tax, an 18% tip and the total charge. As before, first write down your I/O specification and
algorithm in pseudocode. Then implement your algorithm in a Python script named
restaurant.py. Your program should behave as follows:
To round a number to two decimal places, use the format function. For example, to round
3.141592653 to 3.14, use:
format(3.141592653, '.2f')
Run your script with five test cases, and save the output of your test runs in a text file named
restaurant.txt. As done in laboratories, put a file header at the top of your Python script.
4. Invoking functions
Recall that binary numbers are numbers expressed in the base-2 notation using only 0’s and 1’s
(to learn more, watch Professor Ralph Morelli’s introduction and lesson on binary-to-decimal
and decimal-to-binary conversions). Now, using Python’s interactive mode, express the
following integers in the binary notation by invoking an appropriate function from the list of
Built-in Functions in the Python Standard Library.
115
1823
2010
1048576
1048575
The hexadecimal notation, whose base is 16, is another common way to represent numbers in
computing. In this notation, there are 16 possible values for a single digit: 0, 1, 2, 3, 4, 5, 6, 7, 8,
9, A, B, C, D, E, F. Repeat the above exercise to express the given integers in the hexadecimal
notation. Save the output from your interactive session as ex4.txt.
What to hand in
Create a folder by the name “HW1” and copy ex1.txt, ex2.txt, ex4.txt and restaurant.txt inside
HW1. Then create a zip file with the name HW1.zip and upload into moodle.