简体   繁体   English

为什么在此行代码中出现错误“字符串索引必须为整数”?

[英]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. 这也发生在InterestListTotalBalance中。

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. 还要注意, totalBalanceInterestfloat对象,而不是列表对象或可迭代对象。 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.

相关问题 为什么我在索引字典时会收到“字符串索引必须是整数”错误? - Why am I getting a “String indices must be integers” error when I am indicing a dictionary? 为什么会出现TypeError:字符串索引必须为整数? - Why am I getting TypeError: string indices must be integers? 为什么我会收到这个 TypeError:字符串索引必须是整数 - why am i getting this TypeError: string indices must be integers 为什么我不断收到“字符串索引必须是整数”错误? - Why do I keep getting a 'string indices must be integers' error? 为什么我会收到有关“字符串索引必须为整数”的错误消息? - Why am I getting this error about “string indices must be integer”? 为什么我的函数会出现“TypeError:字符串索引必须是整数”? - Why am I getting "TypeError: string indices must be integers" on my function? 为什么我看到“TypeError:字符串索引必须是整数”? - Why am I seeing "TypeError: string indices must be integers"? 我收到python错误TypeError:字符串索引必须是整数,而不是str - I am getting python error TypeError: String indices must be integers, not str 我为什么会收到此错误TypeError:列表索引必须是整数或切片,而不是str - Why am I getting this error TypeError: list indices must be integers or slices, not str 我收到此错误-&gt; TypeError:字符串索引必须为整数 - I'm getting this error --> TypeError: string indices must be integers
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM