Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit f7e05e0

Browse files
Solutions
1 parent f2689e3 commit f7e05e0

40 files changed

+1225
-0
lines changed

‎CH4/Checkpoints

Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
#4.1
2+
< , > , <= , >= , == , !=
3+
4+
#4.2
5+
i = 1
6+
j = 0
7+
b1 = True
8+
b2 = False
9+
10+
#4.3
11+
random.randint(0, 19)
12+
13+
#4.4
14+
random.randint(10, 19)
15+
16+
#4.5
17+
random.randint(10, 50)
18+
19+
#4.6
20+
random.randint(0, 1)
21+
22+
#4.7
23+
if y >0:
24+
x = 1
25+
26+
#4.8
27+
if score > 90:
28+
pay *= 1.03
29+
30+
#4.9
31+
if score > 90:
32+
pay *= 1.03
33+
eles:
34+
pay *= 1.01
35+
36+
#4.10
37+
(a): is even.
38+
is odd.
39+
(b): is odd.
40+
41+
#4.11
42+
if x =3 and y =2 --> x is 3
43+
if x =3 and y =4 --> z is 7
44+
if x =2 and y =2 --> no output
45+
46+
#4.12
47+
if x = 2 and y = 4 --> x is 2
48+
if x = 3 and y = 2 --> no output
49+
if x = 3 and y = 3 --> z is 6
50+
51+
#4.13
52+
The conditions are tested in the wrong orders
53+
54+
#4.14
55+
A and C are equivalent, but B and D are incorrectly indented.
56+
57+
#4.15
58+
newLine = (count % 10 == 0)
59+
60+
#4.16
61+
Both are correct. B is better
62+
Both conditions in A are tested. In B the condition is tested only once.
63+
64+
#4.17
65+
in code (a):
66+
if number = 14 --> 14 is even
67+
if number = 15 --> 15 is multiple of 5
68+
if number = 30 --> 30 is is even
69+
30 is multiple of 5
70+
in code (b):
71+
if number = 14 --> 14 is even
72+
if number = 15 --> 15 is multiple of 5
73+
if number = 30 --> 30 is is even
74+
75+
#4.18
76+
yes
77+
78+
#4.19
79+
Causes a runtime error, because tax will not be created.
80+
81+
#4.20
82+
True and (3 > 4) --> False
83+
not (x > 0) and (x > 0) --> False
84+
(x > 0) or (x < 0) --> True
85+
(x != 0) or (x == 0) --> True
86+
(x >= 0) or (x < 0) --> True
87+
(x != 1) == not (x == 1) --> True
88+
89+
#4.21
90+
(num > 1) and (num < 100)
91+
92+
#4.22
93+
((num > 1) and (num < 100)) or (num < 0)
94+
95+
#4.23
96+
x >= y >= 0 --> False
97+
x <= y >= 0 --> True
98+
x != y == 5 --> True
99+
(x != 0) or (x == 0) --> True
100+
101+
#4.24
102+
Yes
103+
104+
#4.25
105+
if ch = 'A' --> True
106+
if ch = 'p' --> False
107+
if ch = 'E' --> True
108+
if ch = '5' --> False
109+
110+
#4.26
111+
(x < y and y < z) is True
112+
(x < y or y < z) is True
113+
not (x < y) is False
114+
(x < y < z) is True
115+
not (x < y < z) is False
116+
117+
#4.27
118+
age > 13 and age < 18
119+
120+
#4.28
121+
weight > 50 or height > 160
122+
123+
#4.29
124+
weight > 50 and height > 160
125+
126+
#4.30
127+
(weight > 50 or height > 160) and not (weight > 50 and height > 160)
128+
129+
#4.31
130+
Sorted
131+
132+
#4.32
133+
(a)ticketPrice = 20 if ages >= 16 else 10
134+
(b)print(count, end = "\n" if count % 10 == 0 else " ")
135+
136+
#4.33
137+
(a) if x >10:
138+
score = 3 * scale
139+
else:
140+
score = 4 * scale
141+
(b) if income > 10000:
142+
tax = income * 0.2
143+
else:
144+
tax = income * 0.17 + 1000
145+
(c) if number % 3 == 0:
146+
print(i)
147+
else:
148+
print(j)
149+
150+
#4.34
151+
The precedence order of the Boolean operators are not, and, or in this order.
152+
True
153+
True
154+
155+
#4.35
156+
True
157+
158+
#4.36
159+
False
160+
False
161+
162+
#4.37
163+
Is (x > 0 and x < 10) the same as ((x > 0) and (x < 10))? Yes
164+
Is (x > 0 or x < 10) the same as ((x > 0) or (x < 10))? Yes
165+
Is (x > 0 or x < 10 and y < 0) the same as (x > 0 or (x < 10 and y < 0))? yes

‎CH4/EX4.1.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# (Algebra: solve quadratic equations) The two roots of a quadratic equation, for
2+
# example, can be obtained using the following formula:
3+
# is called the discriminant of the quadratic equation. If it is positive, the
4+
# equation has two real roots. If it is zero, the equation has one root. If it is negative,
5+
# the equation has no real roots.
6+
# Write a program that prompts the user to enter values for a, b, and c and displays
7+
# the result based on the discriminant. If the discriminant is positive, display two
8+
# roots. If the discriminant is 0, display one root. Otherwise, display The equation
9+
# has no real roots.
10+
import math
11+
12+
a, b, c = eval(input("Enter a, b, c: "))
13+
14+
discriminant = math.pow(b, 2) - 4 * a * c
15+
16+
if discriminant > 0:
17+
r1 = (-b + math.sqrt(b ** 2 - 4 * a * c)) / (2 * a)
18+
r2 = (-b - math.sqrt(b ** 2 - 4 * a * c)) / (2 * a)
19+
print("The roots are", format(r1, "0.6f"), format(r2, "0.6f"))
20+
elif discriminant == 0:
21+
r1 = (-b + math.sqrt(b ** 2 - 4 * a * c)) / (2 * a)
22+
print("The root is", format(r1, "0.6f"))
23+
else:
24+
print("The equation has no real roots")

‎CH4/EX4.10.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# (Game: multiplication quiz) Listing 4.4, SubtractionQuiz.py, randomly generates
2+
# a subtraction question. Revise the program to randomly generate a multiplication
3+
# question with two integers less than 100.
4+
5+
import random
6+
7+
n1, n2 = random.randint(0, 99), random.randint(0, 99)
8+
s = "What is " + str(n1) + " * " + str(n2) + " ? "
9+
res = eval(input(s))
10+
11+
if res == (n1 * n2):
12+
print("You are correct!")
13+
else:
14+
print("You are wrong")
15+
print(str(n1), "*", str(n2), "=", str(n1 * n2))

‎CH4/EX4.11.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# (Find the number of days in a month) Write a program that prompts the user to
2+
# enter the month and year and displays the number of days in the month. For example,
3+
# if the user entered month 2 and year 2000, the program should display that
4+
# February 2000 has 29 days. If the user entered month 3 and year 2005, the program
5+
# should display that March 2005 has 31 days.
6+
7+
month, year = eval(input("Enter month and year: "))
8+
days = 0
9+
isLeapYear = (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0)
10+
11+
if month == 1:
12+
month = "January"
13+
days = 31
14+
elif month == 2:
15+
month = "February"
16+
if isLeapYear:
17+
days = 29
18+
else:
19+
days = 28
20+
elif month == 3:
21+
month = "March"
22+
days = 31
23+
elif month == 4:
24+
month = "April"
25+
days = 30
26+
elif month == 5:
27+
month = "May"
28+
days = 31
29+
elif month == 6:
30+
month = "June"
31+
days = 30
32+
elif month == 7:
33+
month = "July"
34+
days = 31
35+
elif month == 8:
36+
month = "Augustus"
37+
days = 30
38+
elif month == 9:
39+
month = "September"
40+
days = 31
41+
elif month == 10:
42+
month = "October"
43+
days = 30
44+
elif month == 11:
45+
month = "November"
46+
days = 31
47+
elif month == 12:
48+
month = "December"
49+
days = 30
50+
51+
print(month, year, "has", days, "days")

‎CH4/EX4.12.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# 4.12 (Check a number) Write a program that prompts the user to enter an integer and
2+
# checks whether the number is divisible by both 5 and 6, divisible by 5 or 6, or just
3+
# one of them (but not both).
4+
5+
n = int(input("Enter an integer: "))
6+
7+
print("Is", n, " divisible by 5 and 6?", n % 5 == 0 and n % 6 == 0)
8+
print("Is", n, "divisible by 5 or 6?", n % 5 == 0 or n % 6 == 0)
9+
print("Is", n, "divisible by 5 or 6, but not both?", (n % 5 == 0 and n % 6 != 0) or (n % 5 != 0 and n % 6 == 0))

‎CH4/EX4.13.py

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
# 4.13 (Financial application: compute taxes) Listing 4.7, ComputeTax.py, gives the
2+
# source code to compute taxes for single filers. Complete Listing 4.7 to give the
3+
# complete source code for the other filing statuses.
4+
import sys
5+
6+
# Prompt the user to enter filing status
7+
status = eval(input(
8+
"(0-single filer, 1-married jointly,\n" +
9+
"2-married separately, 3-head of household)\n" +
10+
"Enter the filing status: "))
11+
12+
# Prompt the user to enter taxable income
13+
income = eval(input("Enter the taxable income: "))
14+
15+
# Compute tax
16+
tax = 0
17+
18+
if status == 0: # Compute tax for single filers
19+
if income <= 8350:
20+
tax = income * 0.10
21+
elif income <= 33950:
22+
tax = 8350 * 0.10 + (income - 8350) * 0.15
23+
elif income <= 82250:
24+
tax = 8350 * 0.10 + (33950 - 8350) * 0.15 + \
25+
(income - 33950) * 0.25
26+
elif income <= 171550:
27+
tax = 8350 * 0.10 + (33950 - 8350) * 0.15 + \
28+
(82250 - 33950) * 0.25 + (income - 82250) * 0.28
29+
elif income <= 372950:
30+
tax = 8350 * 0.10 + (33950 - 8350) * 0.15 + \
31+
(82250 - 33950) * 0.25 + (171550 - 82250) * 0.28 + \
32+
(income - 171550) * 0.33
33+
else:
34+
tax = 8350 * 0.10 + (33950 - 8350) * 0.15 + \
35+
(82250 - 33950) * 0.25 + (171550 - 82250) * 0.28 + \
36+
(372950 - 171550) * 0.33 + (income - 372950) * 0.35
37+
elif status == 1: # Compute tax for married file jointly
38+
if income <= 16700:
39+
tax = income * 0.10
40+
elif income <= 67900:
41+
tax = 16700 * 0.10 + (income - 16700) * 0.15
42+
elif income <= 137050:
43+
tax = 16700 * 0.10 + (67900 - 16700) * 0.15 + \
44+
(income - 67900) * 0.25
45+
elif income <= 208850:
46+
tax = 16700 * 0.10 + (67900 - 16700) * 0.15 + \
47+
(137050 - 6700) * 0.25 + (income - 137050) * 0.28
48+
elif income <= 372950:
49+
tax = 16700 * 0.10 + (67900 - 16700) * 0.15 + \
50+
(137050 - 67900) * 0.25 + (208850 - 137050) * 0.28 + \
51+
(income - 208850) * 0.33
52+
else:
53+
tax = 16700 * 0.10 + (67900 - 16700) * 0.15 + \
54+
(137050 - 67900) * 0.25 + (208850 - 137050) * 0.28 + \
55+
(372950 - 208850) * 0.33 + (income - 372950) * 0.35
56+
elif status == 2: # Compute tax for married separately
57+
if income <= 8350:
58+
tax = income * 0.10
59+
elif income <= 33950:
60+
tax = 8350 * 0.10 + (income - 8350) * 0.15
61+
elif income <= 68525:
62+
tax = 8350 * 0.10 + (33950 - 8350) * 0.15 + \
63+
(income - 33950) * 0.25
64+
elif income <= 104425:
65+
tax = 8350 * 0.10 + (33950 - 8350) * 0.15 + \
66+
(68525 - 33950) * 0.25 + (income - 68525) * 0.28
67+
elif income <= 186475:
68+
tax = 8350 * 0.10 + (33950 - 8350) * 0.15 + \
69+
(68525 - 33950) * 0.25 + (104425 - 68525) * 0.28 + \
70+
(income - 104425) * 0.33
71+
else:
72+
tax = 8350 * 0.10 + (33950 - 8350) * 0.15 + \
73+
(68525 - 33950) * 0.25 + (104425 - 68525) * 0.28 + \
74+
(186475 - 104425) * 0.33 + (income - 186475) * 0.35
75+
76+
elif status == 3: # Compute tax for head of household
77+
if income <= 11950:
78+
tax = income * 0.10
79+
elif income <= 45500:
80+
tax = 11950 * 0.10 + (income - 11950) * 0.15
81+
elif income <= 117450:
82+
tax = 11950 * 0.10 + (45500 - 11950) * 0.15 + \
83+
(income - 45500) * 0.25
84+
elif income <= 190200:
85+
tax = 11950 * 0.10 + (45500 - 11950) * 0.15 + \
86+
(117450 - 45500) * 0.25 + (income - 117450) * 0.28
87+
elif income <= 372950:
88+
tax = 11950 * 0.10 + (45500 - 11950) * 0.15 + \
89+
(117450 - 45500) * 0.25 + (104425 - 117450) * 0.28 + \
90+
(income - 192200) * 0.33
91+
else:
92+
tax = 11950 * 0.10 + (45500 - 11950) * 0.15 + \
93+
(117450 - 45500) * 0.25 + (192200 - 117450) * 0.28 + \
94+
(186475 - 192200) * 0.33 + (income - 372950) * 0.35
95+
else:
96+
print("Error: invalid status")
97+
sys.exit()
98+
99+
# Display the result
100+
print("Tax is", format(tax, ".2f"))

‎CH4/EX4.14.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# 4.14 (Game: heads or tails) Write a program that lets the user guess whether a flipped
2+
# coin displays the head or the tail. The program randomly generates an integer 0 or
3+
# 1, which represents head or tail. The program prompts the user to enter a guess
4+
# and reports whether the guess is correct or incorrect.
5+
import random
6+
7+
g = random.randint(0, 1)
8+
guess = int(input("Guess Head(0) or Tail(1): "))
9+
10+
if g == guess:
11+
print("You are right. The guess is ", end="Head" if g == 0 else "Tail")
12+
else:
13+
print("Wrong. The guess is ", end="Head" if g == 0 else "Tail")

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /