[英]I'm having a problem with Python programming
首先大家好,我是 stackoverflow 的新手。 如果我有任何錯誤,請告訴我。
我有一個作業要在星期二交,這個作業必須在 Python 完成。但是,我的項目有問題,我會嘗試在這里解決它,因為我不明白問題是什么。 如果你能幫忙,我會很高興。
注意:這個項目是“一個從用戶那里獲取客戶類型和貸款金額並打印前 12 個月貸款金額列表的程序。”
我報錯頁面的整體代碼結構是這樣的:
consumertype = ['mortgage', 'individual', 'corporation']
interestrate = [15.3, 25.2, 20.4]
consumertype = input("Enter your consumer type: ")
loanamount = float(input("Enter your loan amount"))
if consumertype =='mortgage':
print("interst rate of mortgage is", 15.3)
if consumertype =='individual':
print("interest rate of individual is", 25.2)
if consumertype =='corporation':
print("interest rate of corporation is", 20.4)
for i in range(12):
totalinterest = loanamount * (interestrate/100)*(i+1)
totalpayment = totalinterest + loanamount
monthlypayment = totalpayment/12
TimeRange = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
for i in TimeRange:
print ("You will pay", monthlypayment, "$ on", TimeRange, "th.month")
問題:在第 20 行發現錯誤。但是,在我研究的資源中,我意識到不可能在列表中“列出”。 但是我完全不知道如何解決這個錯誤,因為我根本不明白 python。 我只想完成布置的作業,今年畢業。
它在第 20 行的終端中顯示錯誤。這正是第 20 行的代碼:
totalinterest = loanamount * (interestrate/100)*(i+1)
我正在為您分享用可視化代碼截取的屏幕截圖,請查看此屏幕截圖並瀏覽終端。
你能幫我嗎? 從現在開始謝謝你!
您可以更改這對列表
consumertype = ['mortgage', 'individual', 'corporation']
interestrate = [15.3, 25.2, 20.4]
聽寫
interest_rates = {
'mortgage': 15.3,
'individual': 25.2,
'corporation': 20.4
}
然后獲取當前利率而不是
if consumertype =='mortgage':
print("interst rate of mortgage is", 15.3)
if consumertype =='individual':
print("interest rate of individual is", 25.2)
if consumertype =='corporation':
print("interest rate of corporation is", 20.4)
你可以
try:
interestrate = interest_rates[consumertype]
except KeyError:
raise KeyError(f'Invalid consumer type entered: {consumertype}')
然后,對於您剩余的代碼, interstrate
將是一個float
而不是列表來進行計算。
Interestrate 是一個列表,所以有錯誤的行將不起作用。
您需要一種將消費者類型鏈接到利率的方法,這可以通過字典來完成:
interest_rates ={'mortgage': 15.3, 'individual': 25.2, 'corporation': 20.4}
然后,您可以從消費者類型的輸入中獲取利率:
interestrate = interest_rates.get(consumertype)
更正后的代碼:
interest_rates = {'mortgage': 15.3, 'individual': 25.2, 'corporation': 20.4}
consumertype = input("Enter your consumer type: ")
loanamount = float(input("Enter your loan amount"))
interestrate = interest_rates.get(consumertype)
if consumertype == 'mortgage':
print("interst rate of mortgage is", 15.3)
if consumertype == 'individual':
print("interest rate of individual is", 25.2)
if consumertype == 'corporation':
print("interest rate of corporation is", 20.4)
for i in range(12):
totalinterest = loanamount * (interestrate / 100) * (i + 1)
totalpayment = totalinterest + loanamount
monthlypayment = totalpayment / 12
print("You will pay", monthlypayment, "$ on", (i+1), "th.month")
傑克。 您面臨的問題是因為interestrate
是一個列表,因此不能除以int
類型的100
。 我注意到有幾種方法可以改進代碼。 以下是我的建議:
1. 使用字典將 map 的interestrate
列表轉為consumertype
列表。
變化看起來像這樣:
consumertype = ['mortgage', 'individual', 'corporation']
interestrate = [15.3, 25.2, 20.4]
到
interest_rates = {
'mortgage': 15.3,
'individual': 25.2,
'corporation': 20.4
}
2.不要在代碼中使用這么多if
語句。 [除非必要]
從用戶處獲取consumertype
和loanamount
后,使用字典方法獲取該特定consumertype
的interestrate
值。
我建議的更改如下所示:
if consumertype =='mortgage':
print("interst rate of mortgage is", 15.3)
if consumertype =='individual':
print("interest rate of individual is", 25.2)
if consumertype =='corporation':
print("interest rate of corporation is", 20.4)
到
if (interestrate:=interest_rates.get(consumertype, None)):
print("Interest rate of mortgage is", interestrate)
else:
print("Please verify if the right consume type is entered.")
3. 盡可能避免代碼重復。
正如您在代碼中看到的那樣,有兩個循環基本上運行相同的次數。 如果仔細觀察,您會發現可以將這兩個循環結合起來。
所以改變是
for i in range(12):
totalinterest = loanamount * (interestrate/100)*(i+1)
totalpayment = totalinterest + loanamount
monthlypayment = totalpayment/12
TimeRange = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
for i in TimeRange:
print ("You will pay", monthlypayment, "$ on", TimeRange, "th.month")
到
for i in range(12):
totalinterest = loanamount * (interestrate/100)*(i+1)
totalpayment = totalinterest + loanamount
monthlypayment = totalpayment/12
print ("You will pay", monthlypayment, "$ on", i+1, "th.month")
最終代碼
interest_rates = {
'mortgage': 15.3,
'individual': 25.2,
'corporation': 20.4
}
consumertype = input("Enter your consumer type: ")
loanamount = float(input("Enter your loan amount: "))
if (interestrate:=interest_rates.get(consumertype, None)):
print("Interest rate of mortgage is", interestrate)
else:
print("Please verify if the right consume type is entered.")
for i in range(12):
totalinterest = loanamount * (interestrate/100)*(i+1)
totalpayment = totalinterest + loanamount
monthlypayment = totalpayment/12
print ("You will pay", monthlypayment, "$ on", i+1, "th.month")
注意:代碼需要Python3.8或更高版本。
參考:
與該問題相關的主要問題是interestrate
是一個列表而不是數字類型,因此算術運算失敗。 這個問題可以通過使用字典來解決:
interestrate = {'mortgage':15.3, 'individual':25.2, 'corporation':20.4}
並用interestrate
interestrate[consumrtype]
替換 interrate
雖然不是當前問題,但該代碼還有其他幾個已在此代碼中解決的問題:
# Using Dicts Instead of a list to prevent extra time consumption in searching
interestrate = {'mortgage':15.3, 'individual':25.2, 'corporation':20.4}
consumertype = input("Enter your consumer type: ")
loanamount = float(input("Enter your loan amount"))
if consumertype =='mortgage':
print("interst rate of mortgage is", 15.3)
if consumertype =='individual':
print("interest rate of individual is", 25.2)
if consumertype =='corporation':
print("interest rate of corporation is", 20.4)
for i in range(12):
# Here interestrate[consumertype] refers to the interest rate of the consumer
# type entered by the user
totalinterest = loanamount * (interestrate[consumertype]/100)*(i+1)
totalpayment = totalinterest + loanamount
monthlypayment = totalpayment/12
# There is no need of using a another function as it will unnecessarily increase
# the complexity, so Here we display the interest at the same time as
# calculating it
print ("You will pay", monthlypayment, "$ on", i+1, "th.month")
由於您的輸入之一是“消費者類型”,我猜測您正在嘗試顯示消費者類型和一年中每個月的每月支付金額,給定與該消費者類型相關聯的利率。
我建議您創建一個字典,將消費者類型與利率聯系起來,如下所示:
Interest_Dict={"Mortgage": 15.3, "Individual": 25.2, "Corporation": 20.4}
然后在計算每月還款額的這部分代碼中,您必須用 Interest_Dict[consumertype] 替換 interestrate
for i in range(12):
totalinterest = loanamount * (Interest_Dict[consumertype]/100)*(i+1)
totalpayment = totalinterest + loanamount
monthlypayment = totalpayment/12
您用於獲取每月付款金額的 for 循環不正確。 您需要更改 for 循環,但根據您的公式,您根本不需要循環。
可以使用以下任何公式直接找到每月支付金額: https://www.thebalance.com/loan-payment-calculations-315564#:~:text=To%20calculate%20the%20monthly%20payment,per% 20年%20次%2030%20年)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.