[英]Why am I getting the error 'string indices must be integers' in this line of code?
okay so i know this probably doesn't look very tidy, but on the line 'FinalMessage' I am getting the error 'string indices must be integers' and I need a little help putting it right and understanding why. 好的,所以我知道这看起来可能不太整洁,但是在“ FinalMessage”行上,我得到了“字符串索引必须是整数”的错误,我需要一点帮助将其正确并理解原因。 Any help appreciated :)
任何帮助表示赞赏:)
StartBalance = input("Enter a Start Balance - £")
InterestRate = input("Enter Interest Rate - %")
StartBalance = float(StartBalance)
InterestRate = float(InterestRate)
InterestRate = InterestRate/100
TotalBalance = StartBalance
MonthList = []
InterestList = []
Months = 24
print("Month Interest Total Balance")
for Months in range(0,25):
Interest = TotalBalance*InterestRate/12
TotalBalance = TotalBalance + Interest
if Months < 10:
message = " %d %6.2f %6.2f" %(Months, Interest,
TotalBalance)
else:
message = " %d %6.2f %6.2f" %(Months, Interest,
TotalBalance)
print(message)
MonthList.append(Months)
InterestList.append(Interest)
EndOfInput = False
while EndOfInput == False:
NumOfMonth = input("Enter Month to see Interest and Balance - ")
FinalMessage = float(NumOfMonth[MonthList]), float(Interest[MonthList]),
float(TotalBalance[MonthList])
print(FinalMessage)
Response = ""
while Response != "Yes" and Response != "No":
Response = input("Would you like to see another Month? - ")
if Response == "Yes":
if Response != "Yes" and Response != "No":
print("Invalid input, please enter Yes or No")
if Response == "No":
EndOfInput = True
print("Thank you for using this program, Goodbye :)")
Convert NumOfMonth
to integer by int(NumOfMonth)
通过
int(NumOfMonth)
将NumOfMonth
转换为整数
The line should be: 该行应为:
FinalMessage = float(MonthList[NumOfMonth]), float(InterestList[NumOfMonth]), float(TotalBalance)
Your main problem was that you were getting your list indices mixed up. 您的主要问题是您混淆了列表索引。 You want
NumOfMonth
inside the []
, not outside. 您想将
NumOfMonth
放在[]
而不是外面。 This happened for the InterestList
and TotalBalance
as well. 这也发生在
InterestList
和TotalBalance
中。
In the line 在行中
FinalMessage = float(NumOfMonth[MonthList]), float(Interest[MonthList]), float(TotalBalance[MonthList])
you are using MonthList
as an index which is a list. 您正在使用
MonthList
作为列表的索引。 Also note that totalBalance
and Interest
are float
objects and not a list object or an iterable. 还要注意,
totalBalance
和Interest
是float
对象,而不是列表对象或可迭代对象。 This makes Interest[NumOfMonth]
and TotalBalance[MonthList]
invalid. 这将使
Interest[NumOfMonth]
和TotalBalance[MonthList]
无效。
It should be 它应该是
FinalMessage = float(MonthList[int(NumOfMonth])), InterestList[int(NumOfMonth]), TotalBalance
You define MonthList
as (heh) a list here MonthList = []
. 您可以定义
MonthList
为(港灯)在此列表MonthList = []
Then you try to use it as an index here NumOfMonth[MonthList]
, which predictably fails. 然后,您尝试在此处将其用作索引
NumOfMonth[MonthList]
,该索引NumOfMonth[MonthList]
失败。
I assume you wanted the Xth month, which would translate into: 我假设您想要第X个月,它将转换为:
MonthList[NumOfMonth]
But then you also have a wrong indexation here Interest[MonthList]
, which i again, assume , was meant to be InterestList[NumOfMonth]
但是然后您在这里也有一个错误的索引
Interest[MonthList]
,我再次假设它应该是InterestList[NumOfMonth]
EDIT 编辑
As pointed out in the comments, you'd have to convert NumOfMonth to an int first NumOfMonth=int(NumOfMonth)
正如评论中指出的那样,您必须先将
NumOfMonth=int(NumOfMonth)
转换为int NumOfMonth=int(NumOfMonth)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.