Excercise 1.1
Excercise 1.1
Excercise 1.1
In one particular jurisdiction, drink containers holding one liter or less have a
0.10deposit, anddrinkcontainersholdingmorethanoneliterhavea 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 two digits to the right of the decimal point.
1. 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
1. Write a program that reads a positive integer, n, from the user and then displays the sum of all of the
integers from 1 to n. The sum of the first n positive integers can be
4.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.
In [11]: def compound_interest(principal, rate, time):
# Driver Code
#Taking input from user.
principal = int(input("Enter the principal amount: "))
rate = int(input("Enter rate of interest: "))
time = int(input("Enter time in years: " ))
#Function Call
compound_interest(principal,rate,time)
1. In the United States, fuel efficiency for vehicles is normally expressed in miles-per-gallon (MPG). In
Canada, fuel efficiency is normally expressed in liters-per-hundred kilometers (L/100km). Use your
research skills to determine how to convert from MPG to L/100km.Then create a program that reads a
value from the user in American units and displays the equivalent fuel efficiency in Canadian units.
In [ ]:
1. The surface of the Earth is curved, and the distance between degrees of longitude varies with latitude.
As a result, finding the distance between two points on the surface of the Earth is more complicated
than simply using the Pythagorean theorem.
In [ ]:
1. Many people think about their height in feet and inches, even in some countries that primarily use the
metric system. Write a program that reads a number of feet from the user, followed by a number of
inches. Once these values are read, your program should compute and display the equivalent number
of centimeters.
In [16]: IN_PER_FT = 12
CM_PER_IN = 2.54
print("Enter your height:")
feet = float(input("Number of feet:"))
inches = float(input("Number of inches:"))
cm = ((feet * IN_PER_FT + inches)*CM_PER_IN)/2
print("Your height in centimeters is:", cm)
1. Write a program that begins by reading a radius, , from the user. The program will continue by
computing and displaying the area of a circle with radius and the volume of a sphere with radius . Use
the pi constant in the math or numpy modules in your calculations. Use Internet to look up the
necessary formula if you don't have them memorized.
1. Python’s time module includes several time-related functions. One of these is the asctime function
which reads the current time from the computer’s internal clock and returns it in a human-readable
format. Use this function to write a program that displays the current time and date. Your program will
not require any input from the user
# dd/mm/YY H:M:S
dt_string = now.strftime("%d/%m/%Y %H:%M:%S")
print("date and time =", dt_string)
1. When the wind blows in cold weather, the air feels even colder than it actually is because the
movement of the air increases the rate of cooling for warm objects, like people. This effect is known as
wind chill.
In 2001, Canada, the United Kingdom and the United States adopted the following formula for computing
the wind chill index. Within the formula is the air temperature in degrees Celsius and is the wind speed in
kilometers per hour. A similar formula with different constant values can be used for temperatures in
degrees Fahrenheit and wind speeds in miles per hour.
Write a program that begins by reading the air temperature and wind speed from the user. Once these
values have been read your program should display the wind chill index rounded to the closest integer.
The wind chill index is only considered valid for temperatures less than or equal to 10 degrees Celsius and
wind speeds exceeding 4.8 kilometers per hour.
In [41]: WC_OFFSET = 13.12
WC_FACTOR1 = 0.6215
WC_FACTOR2 = -11.37
WC_FACTOR3 = 0.3956
WC_EXPONENT = 0.16
temp = float(input("Air temperature (degree Celsius): "))
speed = float(input("Wind speed (kilometers per hour): "))
wci = WC_OFFSET + \
WC_FACTOR1 * temp+ \
WC_FACTOR2 *speed **WC_EXPONENT +\
WC_FACTOR3 * temp*speed**WC_EXPONENT
print("The wind chill index is", round(wci))
1. Develop a program that reads a four-digit integer from the user and displays the sum of its digits. For
example, if the user enters 3141 then your program should display 3 + 1 + 4 + 1 = 9.
sum = 0
for digit in str(n):
sum += int(digit)
return sum
print(getSum(n))
1. Create a program that reads three integers from the user and displays them in sorted order (from
smallest to largest). Use the min and max functions to find the smallest and largest values. The middle
value can be found by computing the sum of all three values, and then subtracting the minimum value
and the maximum value.
1. A bakery sells loaves of bread for $3.49 each. Day old bread is discounted by 60 percent. Write a
program that begins by reading the number of loaves of day old bread being purchased from the user.
Then your program should display the regular price for the bread, the discount because it is a day old,
and the total price. Each of these amounts should be displayed on its own line with an appropriate
label. All of the values should be displayed using two decimal places, and the decimal points in all of
the numbers should be aligned when reasonable values are entered by the user.
In [ ]:
In [ ]:
In [ ]:
In [ ]: