Programming Exercises

P1:

Write a Python program that prompts the user for two integer values and displays the result of the first number divided by the second, with exactly two decimal places displayed.


Solution:


In [2]:
a = int(input("Enter first integer: "))


Enter first integer: 19

In [3]:
b = int(input("Enter second integer: "))


Enter second integer: 7

In [4]:
print("Result is :", format((a/b), '.2f'))


Result is : 2.71

P2:

Write a Python program that prompts the user for two floating-point values and displays the result of the first number divided by the second, with exactly six decimal places displayed.


Solution:


In [5]:
a = float(input("Enter first float: "))


Enter first float: 24.5

In [6]:
b = float(input("Enter second float: "))


Enter second float: 3.78

In [7]:
print("Result is :", format((a/b), '.6f'))


Result is : 6.481481

P3:

Write a Python program that prompts the user to enter an upper or lower case letter and displays the corresponding ASCII encoding.

Solution:


In [8]:
inp = input("Enter a upper or lower case letter: ")


Enter a upper or lower case letter: K

In [9]:
print("Here is the ASCII encoding of your number: " + str(ord(inp)))


Here is the ASCII encoding of your number: 75

Development Problems

D1: Losing Your Head over Chess

The game of chess is generally believed to have been invented in India in the sixth century for a ruling
king by one of his subjects. The king was supposedly very delighted with the game and asked the sub-
ject what he wanted in return. The subject, being clever, asked for one grain of wheat on the fi rst
square, two grains of wheat on the second square, four grains of wheat on the third square, and so forth,
doubling the amount on each next square. The king thought that this was a modest reward for such an
invention. However, the total amount of wheat would have been more than 1,000 times the current
world production.

Develop and test a Python program that calculates how much wheat this would be in pounds, using
the fact that a grain of wheat weighs approximately 1/7,000 of a pound.

Solution:


In [16]:
print(format((sum([2**i for i in range(64)])/7000), '.1f'))


2635249153387079.0

D2: Life Signs

Develop and test a program that determines approximately how many breaths and how many heartbeats the 20 year old person has had in their life. The average respiration (breath) rate of people changes during different stages of development. Use the breath rates given below for use in your program:

                     Breaths per Minute
Infant               30–60 
1–4 years            20–30 
5–14 years           15–25     
adults               12–20
For heart rate, use an average of 67.5 beats per second.

Solution:


In [17]:
age = 20
total_breaths = 45*60*24*365
age -= 1
total_breaths += 25*60*24*365*3
age -= 3
total_breaths += 20*60*24*365*9
age -= 9
total_breaths += 16*60*24*365*7

heart_beats = 67.5*60*60*24*365*20

print("20 years old person had breath approximately: ", total_breaths , " times")
print("20 years old person had heartbeats approximately: ", heart_beats , " times")


20 years old person had breath approximately:  216547200  times
20 years old person had heartbeats approximately:  42573600000.0  times