为什么我在这行代码中得到错误'字符串索引必须是整数'?

时间:2017-08-18 10:55:45

标签: python string integer indices

好吧所以我知道这可能看起来不是很整洁,但是在'FinalMessage'这行我得到错误'字符串索引必须是整数'我需要一些帮助正确理解为什么。任何帮助表示赞赏:)

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 :)")

3 个答案:

答案 0 :(得分:2)

NumOfMonth转换为整数int(NumOfMonth)

该行应为:

FinalMessage = float(MonthList[NumOfMonth]), float(InterestList[NumOfMonth]), float(TotalBalance)

你的主要问题是你的列表索引混乱了。您希望NumOfMonth内的[],而不是外部InterestList。这也发生在TotalBalance<style type="text/css"> .TFtableCol{ width:100%; border-collapse:collapse; } .TFtableCol td{ padding:7px; border:#4e95f4 1px solid; } /* improve visual readability for IE8 and below */ .TFtableCol tr{ background: #b8d1f3; } /* Define the background color for all the ODD table columns */ .TFtableCol tr td:nth-child(odd){ background: #b8d1f3; } /* Define the background color for all the EVEN table columns */ .TFtableCol tr td:nth-child(even){ background: #dae5f4; } </style> <table class="TFtableCol"> <tr><td>Text</td><td>Text</td><td>Text</td><td>Text</td></tr> <tr><td>Text</td><td>Text</td><td>Text</td><td>Text</td></tr> <tr><td>Text</td><td>Text</td><td>Text</td><td>Text</td></tr> <tr><td>Text</td><td>Text</td><td>Text</td><td>Text</td></tr> </table>上。

答案 1 :(得分:0)

在第

FinalMessage = float(NumOfMonth[MonthList]), float(Interest[MonthList]), float(TotalBalance[MonthList])  

您使用MonthList作为列表的索引。另请注意,totalBalanceInterestfloat个对象,而不是列表对象或可迭代对象。这会使Interest[NumOfMonth]TotalBalance[MonthList]无效。

应该是

   FinalMessage = float(MonthList[int(NumOfMonth])), InterestList[int(NumOfMonth]), TotalBalance  

答案 2 :(得分:0)

您可以在此MonthListMonthList = []定义为(heh)列表。然后你尝试将它用作NumOfMonth[MonthList]的索引,这可以预测会失败。

我假设您想要第X个月,这将转化为:

MonthList[NumOfMonth]

但是你在这里也有一个错误的索引Interest[MonthList],我再次假设,它应该是InterestList[NumOfMonth]

修改

正如评论中所指出的,您必须先将NumOfMonth转换为int NumOfMonth=int(NumOfMonth)