回答編集履歴

2

c

2019/04/18 03:32

投稿

tachikoma
tachikoma

スコア3601

test CHANGED
@@ -73,3 +73,107 @@
73
73
  print(s.format(**kv))
74
74
 
75
75
  ```
76
+
77
+
78
+
79
+
80
+
81
+ ----
82
+
83
+ キーワード引数を使う場合だと、例えば一つの変数info(dictクラス)に情報を集約しておく、などですかね。
84
+
85
+
86
+
87
+ ```Python
88
+
89
+ # Compute a payment schedule
90
+
91
+
92
+
93
+ DISCOUNT_RATE = 0.1
94
+
95
+ ANNUAL_INTEREST_RATE = 0.12
96
+
97
+ MONTHLY_INTEREST_RATE = ANNUAL_INTEREST_RATE / 12
98
+
99
+ MONTHLY_PAYMENT_RATE = 0.05
100
+
101
+
102
+
103
+ original_price = float(input("Enter the purchase price: "))
104
+
105
+ discounted_price = original_price * \
106
+
107
+ (1 - DISCOUNT_RATE) # The price for members
108
+
109
+
110
+
111
+ info = {
112
+
113
+ "month": 1,
114
+
115
+ "starting": discounted_price, # The current total balance owed
116
+
117
+ # the sum of interest and principal for that manth
118
+
119
+ "monthly_payment": discounted_price * MONTHLY_PAYMENT_RATE,
120
+
121
+ "interest": discounted_price * MONTHLY_INTEREST_RATE,
122
+
123
+ }
124
+
125
+ info["principal"] = info["monthly_payment"] - info["interest"]
126
+
127
+ # The balance remaining after payment
128
+
129
+ info["remaining"] = info["starting"] - info["principal"]
130
+
131
+
132
+
133
+ # display the titles of a table
134
+
135
+ print(
136
+
137
+ "\n"
138
+
139
+ "Month Starting Balance Interest to Pay Principal to Pay Payment Ending Balance\n"
140
+
141
+ "-----------------------------------------------------------------------------------"
142
+
143
+ )
144
+
145
+
146
+
147
+
148
+
149
+ # Display the info of the first month
150
+
151
+ s = "{month:5d} {starting:16.2f} {interest:15.2f} {principal:16.2f} {monthly_payment:7.2f} {remaining:14.2f}"
152
+
153
+ print(s.format(**info))
154
+
155
+
156
+
157
+
158
+
159
+ # Display the info of the second month and later
160
+
161
+ while info["monthly_payment"] <= info["remaining"]:
162
+
163
+ info["month"] += 1
164
+
165
+ info["starting"] = info["remaining"]
166
+
167
+ info["interest"] = info["starting"] * MONTHLY_INTEREST_RATE
168
+
169
+ info["principal"] = info["monthly_payment"] - info["interest"]
170
+
171
+ info["remaining"] = info["starting"] - info["principal"]
172
+
173
+
174
+
175
+ print(s.format(**info))
176
+
177
+
178
+
179
+ ```

1

..

2019/04/18 03:32

投稿

tachikoma
tachikoma

スコア3601

test CHANGED
@@ -13,3 +13,63 @@
13
13
 
14
14
 
15
15
  というように、テンプレートとしての文字列sを一個作って、あとから必要な場所で.formatメソッドを呼ぶ形にすればテンプレートsを使いまわすことができます。
16
+
17
+
18
+
19
+ ----
20
+
21
+ 追記
22
+
23
+
24
+
25
+ print部分を短くするだけなら次のような方法があります。
26
+
27
+
28
+
29
+ 位置引数を使う場合。
30
+
31
+
32
+
33
+ ```Python
34
+
35
+ s = "{:5d} {:16.2f} {:15.2f} {:16.2f} {:7.2f} {:14.2f}"
36
+
37
+
38
+
39
+ values = [1, 2.0, 3.0, 4.0, 5.0, 6.0]
40
+
41
+ print(s.format(*values))
42
+
43
+ ```
44
+
45
+
46
+
47
+ キーワード引数を利用する場合。
48
+
49
+
50
+
51
+ ```Python
52
+
53
+ s = "{month:5d} {starting:16.2f} {interest:15.2f} {principal:16.2f} {monthly_payment:7.2f} {remaining:14.2f}"
54
+
55
+
56
+
57
+ kv = {
58
+
59
+ "month": 1,
60
+
61
+ "starting": 2.0,
62
+
63
+ "interest": 3.0,
64
+
65
+ "principal": 4.0,
66
+
67
+ "monthly_payment": 5.0,
68
+
69
+ "remaining": 6.0
70
+
71
+ }
72
+
73
+ print(s.format(**kv))
74
+
75
+ ```