Excercise 1.1

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

1. 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.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.

In [1]: LESS_DEPOSIT = 0.10


MORE_DEPOSIT = 0.25
less = int(input("How many containers 1 litre or less?"))
more = int(input("How many containers more than 1 litre?"))
refund = less*LESS_DEPOSIT + more* MORE_DEPOSIT
print("Your total refund will be $%.2f."% refund)

How many containers 1 litre or less?23


How many containers more than 1 litre?32
Your total refund will be $10.30.

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

In [9]: TAX_RATE = 0.05


TIP_RATE = 0.18
cost = float(input("Enter the cost of the meal: "))
# Compute the tax and the tip
tax = cost * TAX_RATE
tip = cost * TIP_RATE
total = cost + tax + tip
print(total)

Enter the cost of the meal: 23


28.29

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

In [10]: n = int(input("Enter a positive integer: "))


sm = n*(n+1)/2
print("The sum of the first", n, "positive integers is", sm)

Enter a positive integer: 34


The sum of the first 34 positive integers is 595.0

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

# Calculates compound interest


Amount = principal * (pow((1 + rate / 100), time))
CI = Amount - principal
print("Compound interest is", CI)

# 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)

Enter the principal amount: 7500


Enter rate of interest: 4
Enter time in years: 2
Compound interest is 612.0000000000009

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)

Enter your height:


Number of feet:5.7415
Number of inches:68.8976
Your height in centimeters is: 175.00041199999998

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.

In [35]: import math


import numpy as np
pi = math.pi

r = float(input ("Enter a radius of circle:"))


area = pi*r**2
volume = 4/3*pi*r**3
print("The area is:", area)
print("The volume is:", volume)

Enter a radius of circle:4


The area is: 50.26548245743669
The volume is: 268.082573106329

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

In [37]: from datetime import datetime

# datetime object containing current date and time


now = datetime.now()

print("now =", now)

# dd/mm/YY H:M:S
dt_string = now.strftime("%d/%m/%Y %H:%M:%S")
print("date and time =", dt_string)

now = 2023-03-08 17:53:24.825522


date and time = 08/03/2023 17:53:24

In [40]: import time


local = time.localtime()
print("Date and time: {} ".format(time.asctime(local)))

Date and time: Wed Mar 8 18:02:22 2023

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))

Air temperature (degree Celsius): 8


Wind speed (kilometers per hour): 5
The wind chill index is 7

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.

In [50]: n = int(input("Enter supposed number:"))


def getSum(n):

sum = 0
for digit in str(n):
sum += int(digit)
return sum

print(getSum(n))

Enter supposed number:9999999999


90

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.

In [51]: a = int(input("Enter the first number: "))


b = int(input("Enter the second number: "))
c = int(input("Enter the third number: "))
mn = min(a,b,c)
mx = max(a,b,c)
md = a+b+c-mn-mx
print("The numbers in sorted order are:")
print(" ", mn)
print(" ",md)
print(" ",mx)

Enter the first number: 23


Enter the second number: 43
Enter the third number: 76
The numbers in sorted order are:
23
43
76

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 [53]: BREAD_PRICE = 3.49


DISCOUNT_RATE = 0.60
num_loaves = int(input("Enter the number of day old loaves:"))
regular_price = num_loaves * BREAD_PRICE
discount = regular_price*DISCOUNT_RATE
total = regular_price - discount
print("Regular price: %5.2f" %regular_price)
print("Discount: %5.2f" % discount)
print("Total: %5.2f" %total)

Enter the number of day old loaves:3


Regular price: 10.47
Discount: 6.28
Total: 4.19

In [ ]:

In [ ]:

In [ ]:

In [ ]:

You might also like