1. Introduction to Programming Exercises
The exercises in this chapter are designed to help you develop your analysis skills by providing you with the opportunity to practice breaking small problems down into sequences of steps. In addition, completing these exercises will help you become familiar with Pythons syntax. To complete each exercise you should expect to use some or all of these Python features:
Generate output with print statements
Read input, including casting that input to the appropriate type
Perform calculations involving integers and floating point numbers using Python operators like + , - , * , / , // , % , and **
Call functions residing in the math module
Control how output is displayed using format specifiers
Exercise 1: Mailing Address
( Solved9 Lines )
Create a program that displays your name and complete mailing address formatted in the manner that you would usually see it on the outside of an envelope. Your program does not need to read any input from the user.
Exercise 2: Hello
( 9 Lines )
Write a program that asks the user to enter his or her name. The program should respond with a message that says hello to the user, using his or her name.
Exercise 3: Area of a Room
( Solved13 Lines )
Write a program that asks the user to enter the width and length of a room. Once the values have been read, your program should compute and display the area of the room. The length and the width will be entered as floating point numbers. Include units in your prompt and output message; either feet or meters, depending on which unit you are more comfortable working with.
Exercise 4: Area of a Field
( Solved15 Lines )
Create a program that reads the length and width of a farmers field from the user in feet. Display the area of the field in acres.
Hint: There are 43,560 square feet in an acre.
Exercise 5: Bottle Deposits
( Solved15 Lines )
In many jurisdictions a small deposit is added to drink containers to encourage people to recycle them. In one particular jurisdiction, drink containers holding one liter or less have a $0.10 deposit, and drink containers holding more than one liter have a $0.25 deposit.
Write a program that reads the number of containers of each size from the user. Your program should continue by computing and displaying the refund that will be received for returning those containers. Format the output so that it includes a dollar sign and always displays exactly two decimal places.
Exercise 6: Tax and Tip
( Solved17 Lines )
The program that you create for this exercise will begin by reading the cost of a meal ordered at a restaurant from the user. Then your program will compute the tax and tip for the meal. Use your local tax rate when computing the amount of tax owing. Compute the tip as 18 percent of the meal amount (without the tax). The output from your program should include the tax amount, the tip amount, and the grand total for the meal including both the tax and the tip. Format the output so that all of the values are displayed using two decimal places.
Exercise 7: Sum of the First
Positive Integers
( Solved12 Lines )
Write a program that reads a positive integer,
, from the user and then displays the sum of all of the integers from 1 to
. The sum of the first
positive integers can be computed using the formula:
Exercise 8: Widgets and Gizmos
( 15 Lines )
An online retailer sells two products: widgets and gizmos. Each widget weighs 75 grams. Each gizmo weighs 112 grams. Write a program that reads the number of widgets and the number of gizmos in an order from the user. Then your program should compute and display the total weight of the order.
Exercise 9: Compound Interest
( 19 Lines )
Pretend that you have just opened a new savings account that earns 4 percent interest per year. The interest that you earn is paid at the end of the year, and is added to the balance of the savings account. Write a program that begins by reading the amount of money deposited into the account from the user. Then your program should compute and display the amount in the savings account after 1, 2, and 3 years. Display each amount so that it is rounded to 2 decimal places.
Exercise 10: Arithmetic
( Solved20 Lines )
Create a program that reads two integers,
and
, from the user. Your program should compute and display:
The sum of
and
The difference when
is subtracted from
The product of
and
The quotient when
is divided by
The remainder when
is divided by
The result of
The result of
Hint: You will probably find the log10 function in the math module helpful for computing the second last item in the list.
Exercise 11: Fuel Efficiency
( 13 Lines )