質問編集履歴
2
追記
test
CHANGED
File without changes
|
test
CHANGED
@@ -45,8 +45,6 @@
|
|
45
45
|
|
46
46
|
|
47
47
|
```
|
48
|
-
|
49
|
-
# Programming Exercise 3.10
|
50
48
|
|
51
49
|
# Compute a payment schedule
|
52
50
|
|
@@ -142,7 +140,7 @@
|
|
142
140
|
|
143
141
|
|
144
142
|
|
145
|
-
|
143
|
+
|
146
144
|
|
147
145
|
# Evaluate the remaining and display the info of the last month
|
148
146
|
|
1
追記
test
CHANGED
File without changes
|
test
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
# f-stringの変数化
|
2
|
+
|
1
3
|
下記のようにf-stringを用いて複数の変数を表示するのですが、同じコードを条件分岐などで複数回使います。
|
2
4
|
|
3
5
|
|
@@ -31,3 +33,139 @@
|
|
31
33
|
|
32
34
|
|
33
35
|
f-stringのコードを変数として定義することはできないのでしょうか?
|
36
|
+
|
37
|
+
|
38
|
+
|
39
|
+
# 追記
|
40
|
+
|
41
|
+
プログラムは、リボ払いでの各月の金利や支払額を表のように出力するものです。
|
42
|
+
|
43
|
+
month, startingなどはprintするごとに変化します。
|
44
|
+
|
45
|
+
|
46
|
+
|
47
|
+
```
|
48
|
+
|
49
|
+
# Programming Exercise 3.10
|
50
|
+
|
51
|
+
# Compute a payment schedule
|
52
|
+
|
53
|
+
|
54
|
+
|
55
|
+
DISCOUNT_RATE = 0.1
|
56
|
+
|
57
|
+
ANNUAL_INTEREST_RATE = 0.12
|
58
|
+
|
59
|
+
MONTHLY_INTEREST_RATE = ANNUAL_INTEREST_RATE / 12
|
60
|
+
|
61
|
+
MONTHLY_PAYMENT_RATE = 0.05
|
62
|
+
|
63
|
+
|
64
|
+
|
65
|
+
original_price = float(input("Enter the purchase price: "))
|
66
|
+
|
67
|
+
discounted_price = original_price * (1 - DISCOUNT_RATE) # The price for members
|
68
|
+
|
69
|
+
|
70
|
+
|
71
|
+
month = 1
|
72
|
+
|
73
|
+
|
74
|
+
|
75
|
+
starting = discounted_price # The current total balance owed
|
76
|
+
|
77
|
+
monthly_payment = (
|
78
|
+
|
79
|
+
discounted_price * MONTHLY_PAYMENT_RATE
|
80
|
+
|
81
|
+
) # the sum of interest and principal for that manth
|
82
|
+
|
83
|
+
interest = discounted_price * MONTHLY_INTEREST_RATE
|
84
|
+
|
85
|
+
principal = monthly_payment - interest
|
86
|
+
|
87
|
+
remaining = starting - principal # The balance remaining after payment
|
88
|
+
|
89
|
+
|
90
|
+
|
91
|
+
|
92
|
+
|
93
|
+
# display the titles of a table
|
94
|
+
|
95
|
+
print(
|
96
|
+
|
97
|
+
"\n"
|
98
|
+
|
99
|
+
"Month Starting Balance Interest to Pay Principal to Pay Payment Ending Balance\n"
|
100
|
+
|
101
|
+
"-----------------------------------------------------------------------------------"
|
102
|
+
|
103
|
+
)
|
104
|
+
|
105
|
+
|
106
|
+
|
107
|
+
|
108
|
+
|
109
|
+
# Display the info of the first month
|
110
|
+
|
111
|
+
print(
|
112
|
+
|
113
|
+
f"{month:5d} {starting:16.2f} {interest:15.2f} {principal:16.2f} {monthly_payment:7.2f} {remaining:14.2f}"
|
114
|
+
|
115
|
+
)
|
116
|
+
|
117
|
+
|
118
|
+
|
119
|
+
|
120
|
+
|
121
|
+
# Display the info of the second month and later
|
122
|
+
|
123
|
+
while monthly_payment <= remaining:
|
124
|
+
|
125
|
+
month += 1
|
126
|
+
|
127
|
+
starting = remaining
|
128
|
+
|
129
|
+
interest = starting * MONTHLY_INTEREST_RATE
|
130
|
+
|
131
|
+
principal = monthly_payment - interest
|
132
|
+
|
133
|
+
remaining = starting - principal
|
134
|
+
|
135
|
+
|
136
|
+
|
137
|
+
print(
|
138
|
+
|
139
|
+
f"{month:5d} {starting:16.2f} {interest:15.2f} {principal:16.2f} {monthly_payment:7.2f} {remaining:14.2f}"
|
140
|
+
|
141
|
+
)
|
142
|
+
|
143
|
+
|
144
|
+
|
145
|
+
s = "{month:5d} {starting:16.2f} {interest:15.2f} {principal:16.2f} {monthly_payment:7.2f} {remaining:14.2f}"
|
146
|
+
|
147
|
+
# Evaluate the remaining and display the info of the last month
|
148
|
+
|
149
|
+
if monthly_payment >= remaining:
|
150
|
+
|
151
|
+
month += 1
|
152
|
+
|
153
|
+
starting = remaining
|
154
|
+
|
155
|
+
interest = 0
|
156
|
+
|
157
|
+
principal = starting
|
158
|
+
|
159
|
+
monthly_payment = starting
|
160
|
+
|
161
|
+
remaining = 0
|
162
|
+
|
163
|
+
|
164
|
+
|
165
|
+
print(
|
166
|
+
|
167
|
+
f"{month:5d} {starting:16.2f} {interest:15.2f} {principal:16.2f} {monthly_payment:7.2f} {remaining:14.2f}"
|
168
|
+
|
169
|
+
)
|
170
|
+
|
171
|
+
```
|