Skip to content

Commit d659862

Browse files
committed
upload code
1 parent 3536ef2 commit d659862

File tree

10 files changed

+184
-0
lines changed

10 files changed

+184
-0
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
"""
2+
Program: Page73_CaseStudy_01.py
3+
Author: Pham Dinh Nam
4+
Compute an investment report.
5+
1. The inputs are starting investment amount number of years interest rate (an integer percent)
6+
2. The report is displayed in tabular form with a header.
7+
3. Computations and outputs:
8+
for each year compute the interest and add it to the investment print a formatted row of results for that year
9+
4. The ending investment and interest earned are also displayed.
10+
"""
11+
12+
# Accept the inputs
13+
startBalance = float(input("Enter the investment amount: "))
14+
years = int(input("Enter the number of years: "))
15+
rate = int(input("Enter the rate as a %: "))
16+
17+
# Convert the rate to a decimal number
18+
rate = rate / 100
19+
20+
# Initialize the accumulator for the interest
21+
totalInterest = 0.0
22+
23+
# Display the header for the table
24+
print("%4s%18s%10s%16s" % ("Year", "Starting balance","Interest", "Ending balance"))
25+
26+
# Compute and display the results for each year
27+
for year in range(1, years + 1):
28+
interest = startBalance * rate
29+
endBalance = startBalance + interest
30+
print("%4d%18.2f%10.2f%16.2f" % (year, startBalance, interest, endBalance))
31+
startBalance = endBalance
32+
totalInterest += interest
33+
34+
# Display the totals for the period
35+
print("Ending balance: $%0.2f" % endBalance)
36+
print("Total interest earned: $%0.2f" % totalInterest)
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
"""
2+
Program: Page93_CaseStudy_01.py
3+
Author: Pham Dinh Nam
4+
5+
Compute the square root of a number.
6+
1. The input is a number.
7+
2. The outputs are the program's estimate of the square root
8+
using Newton's method of successive approximations, and
9+
Python's own estimate using math.sqrt.
10+
"""
11+
12+
13+
import math
14+
15+
# Receive the input number from the user
16+
x = float(input("Enter a positive number: "))
17+
18+
# Initialize the tolerance and estimate
19+
tolerance = 0.000001
20+
estimate = 1.0
21+
22+
# Perform the successive approximations
23+
while True:
24+
estimate = (estimate + x / estimate) / 2
25+
difference = abs(x - estimate ** 2)
26+
print("estimate = ", estimate)
27+
print("difference = ", difference)
28+
if difference <= tolerance:
29+
break
30+
31+
# Output the result
32+
print("The program's estimate:", estimate)
33+
print("Python's estimate: ", math.sqrt(x))
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
"""
2+
Author: Pham Dinh Nam
3+
Date: 02/10/2021
4+
Problem:
5+
Write the outputs of the following loops:
6+
a. for count in range(5):
7+
print(count + 1, end = " ")
8+
b. for count in range(1, 4):
9+
print(count, end = " ")
10+
c. for count in range(1, 6, 2):
11+
print(count, end = " ")
12+
d. for count in range(6, 1, –1):
13+
print(count, end = " ")
14+
Solution:
15+
a. 1 2 3 4 5
16+
b. 1 2 3
17+
c. 1 3 5
18+
d. 6 5 4 3 2
19+
"""
20+
21+
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
"""
2+
Author: Pham Dinh Nam
3+
Date: 02/10/2021
4+
Problem:
5+
Write a loop that prints your name 100 times. Each output should begin on a new line.
6+
Solution:
7+
8+
"""
9+
10+
name = input("Nhap ten cua ban:")
11+
for count in range(101):
12+
print("My name is", name)
13+
print(count)
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
"""
2+
Author: Pham Dinh Nam
3+
Date: 02/10/2021
4+
Problem:
5+
Explain the role of the variable in the header of a for loop.
6+
Solution:
7+
When Python executes the type of for loop just discussed, it counts from 0 to the value of the header’s integer expression minus 1.
8+
On each pass through the loop, the header’s variable is bound to the current value of this count.
9+
"""
10+
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
"""
2+
Author: Pham Dinh Nam
3+
Date: 02/10/2021
4+
Problem:
5+
Write a loop that prints the first 128 ASCII values followed by the corresponding characters (see the section on characters in Chapter 2). Be aware that most of the ASCII values in the range “0..31” belong to special control characters with no standard print representation, so you might see strange symbols in the output for these values.
6+
Solution:
7+
8+
"""
9+
10+
for count in range (130):
11+
print("Ky tu so", count, "la", chr(count))
12+
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
"""
2+
Author: Pham Dinh Nam
3+
Date: 02/10/2021
4+
Problem:
5+
Assume that the variable teststring refers to a string. Write a loop that prints
6+
each character in this string, followed by its ASCII value.
7+
Solution:
8+
9+
"""
10+
11+
for character in "Hello World":
12+
print(ord(character), end = " ")
13+
14+
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
"""
2+
Author: Pham Dinh Nam
3+
Date: 02/10/2021
4+
Problem:
5+
Assume that the variable amount refers to 24.325. Write the outputs of the following statements:
6+
a. print("Your salary is $%0.2f" % amount)
7+
b. print("The area is %0.1f" % amount)
8+
c. print("%7f" % amount
9+
Solution:
10+
a. Your salary is $24.32
11+
b. The area is 24.3
12+
c. 24.325000
13+
"""
14+
15+
amount = 24.325
16+
17+
print("Your salary is $%0.2f" % amount)
18+
print("The area is %0.1f" % amount)
19+
print("%7f" % amount)
20+
21+
22+
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
"""
2+
Author: Pham Dinh Nam
3+
Date: 02/10/2021
4+
Problem:
5+
Write a code segment that displays the values of the integers x, y, and z on a single line, such that each value is right-justified with a field width of 6
6+
Solution:
7+
8+
"""
9+
10+
x = int(input("Nhap X: "))
11+
y = int(input("Nhap Y: "))
12+
z = int(input("Nhap Z: "))
13+
14+
# noinspection PyStringFormat
15+
print("-%6s" % x, y, z)
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
"""
2+
Author: Pham Dinh Nam
3+
Date: 02/10/2021
4+
Problem:
5+
Write a format operation that builds a string for the float variable amount that has exactly two digits of precision and a field width of zero.
6+
Solution:
7+
8+
"""

0 commit comments

Comments
 (0)