Homework 1

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 2

Homework 1

Due: 11:59 PM, Monday, September 12, 2022


IMPORTANT! This is an individual assignment. You may discuss broad issues of
interpretation and understanding and general approaches to a solution. However,
conversion to a specific solution or to program code must be your own work. The
assignment is expected to be your work, designed, and coded by you alone. If you need
help, please consult with your instructor or TAs.
1. Printing text
Using Python’s interactive mode, use the print statement to display the following statements
exactly as written:
● Python's syntax can be a bit tricky.
● Claude Shannon's paper titled "A mathematical theory of communication" is a must
read.
Upon completing each exercise, save the output from your interactive session as ex1.txt.
Here is how to save your output in IDLE: While in the interactive mode of IDLE,
Windows
1. Select File and then Save As.
2. Enter a file name in the File name box, e.g., ex1.txt.
3. Select Text files in the Save as type box.
4. Press Save.
Mac OS
1. Select File and then Save As.
2. Enter a file name in the Save As box, e.g., ex1.txt.
3. Find the folder you wish to save your file in the Where box.
4. Press Save.
5. When asked which extension to use, press Use .txt.
2. Python as a calculator
Using Python’s interactive mode, complete the following exercises:

● 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:

Enter the charge for the food: 20


Food: $20.00
Tax: $1.27
Tip: $3.60
Total: $24.87

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.

Plagiarism and academic dishonesty


Remember, under any circumstance, you must not copy part or all of another’s work and present
it as your own. For more details, read our course policy on plagiarism and academic dishonesty
in the course syllabus.

You might also like