Exercise 02.1.1-Extra

You might also like

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

Task: Consider the software that runs on a self-checkout machine.

One task that it must be able to perform is


to determine how much change to provide when the shopper pays for a purchase with cash. Write a
program that begins by reading a number of cents from the user as an integer. Then your program should
compute and display the denominations of the coins that should be used to give that amount of change to
the shopper. The change should be given using as few coins as possible. Assume that the machine is loaded
with pennies, nickels, dimes, quarters, loonies and toonies.

In [7]: CENTS_PER_TOONIE = 200


CENTS_PER_LOONIE = 100
CENTS_PER_QUARTER = 25
CENTS_PER_DIME = 10
CENTS_PER_NICKEL = 5
cents = int(input("Enter the number of cents: "))
print(" ", cents // CENTS_PER_TOONIE, "toonies")
cents = cents % CENTS_PER_TOONIE
print(" ", cents // CENTS_PER_LOONIE, "loonies")
cents = cents % CENTS_PER_LOONIE
print(" ", cents // CENTS_PER_QUARTER, "quarters")
cents = cents % CENTS_PER_QUARTER
print(" ", cents // CENTS_PER_DIME, "dimes")
cents = cents % CENTS_PER_DIME
print(" ", cents // CENTS_PER_NICKEL, "nickels")
cents = cents % CENTS_PER_NICKEL
# Display the number of pennies
print(" ", cents, "pennies")

Enter the number of cents: 645


3 toonies
0 loonies
1 quarters
2 dimes
0 nickels
0 pennies

1. The amount of energy required to increase the temperature of one gram of a material by one degree
Celsius is the material’s specific heat capacity, C. The total amount of energy, q, required to raise m
grams of a material by ΔT degrees Celsius can be computed using the formula: q = mCΔT Write a
program that reads the mass of some water and the temperature change from the user. Your program
should display the total amount of energy that must be added or removed to achieve the desired
temperature change. Write a program that reads the mass of some water and the temperature change
from the user. Your program should display the total amount of energy that must be added or removed
to achieve the desired temperature change. xtend your program so that it also computes the cost of
heating the water. Electricity is normally billed using units of kilowatt hours rather than Joules. In this
exercise, you should assume that electricity costs 8.9 cents per kilowatt hour. Use your program to
compute the cost of boiling the water needed for a cup of coffee.

In [11]: WATER_HEAT_CAPACITY = 4.186


ELECTRICITY_PRICE = 8.9
J_TO_KWH = 2.777e-7
volume = float(input("Amount of water in milliliters: "))
d_temp = float(input("Temprature increase(degrees Celsius): "))
q = volume *d_temp * WATER_HEAT_CAPACITY
print("That will reqire %d Joules of energy." %q)
kwh = q * J_TO_KWH
cost = kwh * ELECTRICITY_PRICE
print("That much energy will cost %.2f cents." %cost)
Amount of water in milliliters: 170
Temprature increase(degrees Celsius): 30
That will reqire 21348 Joules of energy.
That much energy will cost 0.05 cents.

18.The volume of a cylinder can be computed by multiplying the area of its circular base by its height. Write
a program that reads the radius of the cylinder, along with its height, from the user and computes its
volume. Display the result rounded to one decimal place.

In [14]: pi = 3.14159
radius = float(input("Please enter the Radius of a Cylinder: "))
height = float(input("Please enter the Height of a Cylinder: "))
sa = 2*pi*radius*(radius + height)
Volume = pi*radius *radius*height
L = 2* pi*radius * height
T = pi * radius * radius
print("\n The Surface area of a Cylinder = %.2f" %sa)
print("The Volume of a Cylinder = %.2f" %Volume);
print("Top OR Bottom Surface Area of a Cylinder = %.2f" %T)

Please enter the Radius of a Cylinder: 5


Please enter the Height of a Cylinder: 10

The Surface area of a Cylinder = 471.24


The Volume of a Cylinder = 785.40
Top OR Bottom Surface Area of a Cylinder = 78.54

19.Create a program that determines how quickly an object is travelling when it hits the ground. The user
will enter the height from which the object is dropped in meters (m). Because the object is dropped its initial
speed is 0 m/s. Assume that the acceleration due to gravity is 9.8 m/s2. You can use the formula vf =  v2 i
+ 2ad to compute the final speed, vf , when the initial speed, vi , acceleration, a, and distance, d, are known.

In [15]: from math import sqrt


GRAVITY = 9.8
d = float(input("Height (in meters): "))
vf = sqrt(2*GRAVITY *d)
print("It will hit the ground at %.2f m/s." %vf)

Height (in meters): 10


It will hit the ground at 14.00 m/s.

21.The area of a triangle can be computed using the following formula, where b is the length of the base of
the triangle, and h is its height: area = b × h 2 Write a program that allows the user to enter values for b and
h. The program should then compute and display the area of a triangle with base length b and height h.

In [18]: b = int(input("Input the base: "))


h = int(input("Input the height: "))
area = (b * h)/2
print("area = ", area)

Input the base: 10


Input the height: 8
area = 40.0

In [17]: # second method


# Python Program to find the area of triangle

# Uncomment below to take inputs from the user


a = float(input('Enter first side: '))
b = float(input('Enter second side: '))
c = float(input('Enter third side: '))

# calculate the semi-perimeter


s = (a + b + c) / 2

# calculate the area


area = (s*(s-a)*(s-b)*(s-c)) ** 0.5
print('The area of the triangle is %0.2f' %area)

Enter first side: 10


Enter second side: 12
Enter third side: 14
The area of the triangle is 58.79

23.A polygon is regular if its sides are all the same length and the angles between all of the adjacent sides
are equal. The area of a regular polygon can be computed using the following formula, where s is the length
of a side and n is the number of sides area = n × s2 4 × tan π n  Write a program that reads s and n from
the user and then displays the area of a regular polygon constructed from these values.

In [19]: from math import tan, pi


s = float(input("Enter the lenght of each side: "))
n = int(input("Enter the number of sides: "))
area = (n * s**2)/ (4* tan(pi/n))
print("The area of the polygon is", area)

Enter the lenght of each side: 10


Enter the number of sides: 12
The area of the polygon is 1119.6152422706632

25.In this exercise you will reverse the process described in Exercise 24. Develop a program that begins by
reading a number of seconds from the user. Then your program should display the equivalent amount of
time in the form D:HH:MM:SS, where D, HH, MM, and SS represent days, hours, minutes and seconds
respectively. The hours, minutes and seconds should all be formatted so that they occupy exactly two digits.
Use your research skills determine what additional character needs to be included in the format specifier so
that leading zeros are used instead of leading spaces when a number is formatted to a particular width.

In [24]: SECONDS_PER_DAY = 86400


SECONDS_PER_HOUR = 3600
SECONDS_PER_MINUTE = 60
seconds = int(input("Enter a number of seconds: "))
days = seconds / SECOND_PER_DAY
seconds = seconds % SECONDS_PER_DAY
hours = seconds / SECONDS_PER_HOUR
seconds = seconds % SECONDS_PER_HOUR
minutes = seconds / SECONDS_PER_MINUTE
seconds = seconds % SECONDS_PER_MINUTE
print("The equivalent duration is", \
" %d:%02d:%02d:%02d." % (days, hours, minutes, seconds))

Enter a number of seconds: 13143


The equivalent duration is 0:03:39:03.

28.Write a program that computes the body mass index (BMI) of an individual. Your program should begin
by reading a height and weight from the user. Then it should use one of the following two formulas to
compute the BMI before displaying it. If you read the height in inches and the weight in pounds then body
mass index is computed using the following formula: BMI = weight height × height × 703 If you read the
height in meters and the weight in kilograms then body mass index is computed using this slightly simpler
formula: BMI = weight height × height
In [28]: height = float(input("Enter your height(m): "))
weight = float(input("Enter your weight(kg): "))
print("Your BMI is: ",round(weight/(height * height),2))

Enter your height(m): 1.75


Enter your weight(kg): 75
Your BMI is: 24.49

In [ ]:

You might also like