質問するログイン新規登録

質問編集履歴

2

追記

2019/04/18 02:56

投稿

退会済みユーザー
title CHANGED
File without changes
body CHANGED
@@ -22,7 +22,6 @@
22
22
  month, startingなどはprintするごとに変化します。
23
23
 
24
24
  ```
25
- # Programming Exercise 3.10
26
25
  # Compute a payment schedule
27
26
 
28
27
  DISCOUNT_RATE = 0.1
@@ -70,7 +69,7 @@
70
69
  f"{month:5d} {starting:16.2f} {interest:15.2f} {principal:16.2f} {monthly_payment:7.2f} {remaining:14.2f}"
71
70
  )
72
71
 
73
- s = "{month:5d} {starting:16.2f} {interest:15.2f} {principal:16.2f} {monthly_payment:7.2f} {remaining:14.2f}"
72
+
74
73
  # Evaluate the remaining and display the info of the last month
75
74
  if monthly_payment >= remaining:
76
75
  month += 1

1

追記

2019/04/18 02:56

投稿

退会済みユーザー
title CHANGED
File without changes
body CHANGED
@@ -1,3 +1,4 @@
1
+ # f-stringの変数化
1
2
  下記のようにf-stringを用いて複数の変数を表示するのですが、同じコードを条件分岐などで複数回使います。
2
3
 
3
4
  ```
@@ -14,4 +15,72 @@
14
15
  # print(f"{formatted_output}")
15
16
  ```
16
17
 
17
- f-stringのコードを変数として定義することはできないのでしょうか?
18
+ f-stringのコードを変数として定義することはできないのでしょうか?
19
+
20
+ # 追記
21
+ プログラムは、リボ払いでの各月の金利や支払額を表のように出力するものです。
22
+ month, startingなどはprintするごとに変化します。
23
+
24
+ ```
25
+ # Programming Exercise 3.10
26
+ # Compute a payment schedule
27
+
28
+ DISCOUNT_RATE = 0.1
29
+ ANNUAL_INTEREST_RATE = 0.12
30
+ MONTHLY_INTEREST_RATE = ANNUAL_INTEREST_RATE / 12
31
+ MONTHLY_PAYMENT_RATE = 0.05
32
+
33
+ original_price = float(input("Enter the purchase price: "))
34
+ discounted_price = original_price * (1 - DISCOUNT_RATE) # The price for members
35
+
36
+ month = 1
37
+
38
+ starting = discounted_price # The current total balance owed
39
+ monthly_payment = (
40
+ discounted_price * MONTHLY_PAYMENT_RATE
41
+ ) # the sum of interest and principal for that manth
42
+ interest = discounted_price * MONTHLY_INTEREST_RATE
43
+ principal = monthly_payment - interest
44
+ remaining = starting - principal # The balance remaining after payment
45
+
46
+
47
+ # display the titles of a table
48
+ print(
49
+ "\n"
50
+ "Month Starting Balance Interest to Pay Principal to Pay Payment Ending Balance\n"
51
+ "-----------------------------------------------------------------------------------"
52
+ )
53
+
54
+
55
+ # Display the info of the first month
56
+ print(
57
+ f"{month:5d} {starting:16.2f} {interest:15.2f} {principal:16.2f} {monthly_payment:7.2f} {remaining:14.2f}"
58
+ )
59
+
60
+
61
+ # Display the info of the second month and later
62
+ while monthly_payment <= remaining:
63
+ month += 1
64
+ starting = remaining
65
+ interest = starting * MONTHLY_INTEREST_RATE
66
+ principal = monthly_payment - interest
67
+ remaining = starting - principal
68
+
69
+ print(
70
+ f"{month:5d} {starting:16.2f} {interest:15.2f} {principal:16.2f} {monthly_payment:7.2f} {remaining:14.2f}"
71
+ )
72
+
73
+ s = "{month:5d} {starting:16.2f} {interest:15.2f} {principal:16.2f} {monthly_payment:7.2f} {remaining:14.2f}"
74
+ # Evaluate the remaining and display the info of the last month
75
+ if monthly_payment >= remaining:
76
+ month += 1
77
+ starting = remaining
78
+ interest = 0
79
+ principal = starting
80
+ monthly_payment = starting
81
+ remaining = 0
82
+
83
+ print(
84
+ f"{month:5d} {starting:16.2f} {interest:15.2f} {principal:16.2f} {monthly_payment:7.2f} {remaining:14.2f}"
85
+ )
86
+ ```