【问题标题】:Why am I getting the error 'string indices must be integers' in this line of code?为什么在这行代码中出现错误“字符串索引必须是整数”?
【发布时间】:2018-01-27 01:00:36
【问题描述】:

好的,所以我知道这可能看起来不是很整洁,但是在“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 :)")

【问题讨论】:

  • float(NumOfMonth[MonthList]) 应该类似于 float(MonthList[int(numOfMonth)]
  • 为什么要使用MonthList 作为NumOfMonth 字符串的索引?也许你打算用其他方式来做这件事?但是,您必须先将 NumOfMonth 值转换为整数。

标签: python string integer indices


【解决方案1】:

通过int(NumOfMonth)NumOfMonth转换为整数

该行应该是:

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

您的主要问题是您的列表索引混淆了。你想要NumOfMonth[] 里面,而不是外面。 InterestListTotalBalance 也发生了这种情况。

【讨论】:

    【解决方案2】:

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

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

    MonthList[NumOfMonth]

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

    编辑

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

    【讨论】:

    • 我认为它会是 (NumOfMonth[MonthList]) 因为我试图让它获取用户输入的数字(比如 4)并在表中找到 april 然后打印第 (4) 个月,获得利息,然后是总余额。
    • 哦,我明白了,但是当我将 int() 放在 NumOfMonth 之前时,它现在告诉我 int 不可下标?
    • @Emily:是的,但是你的语法倒退了。 &lt;object&gt;[...] 会将括号左侧的对象视为容器,您在该容器中查找的内容必须位于括号之间。
    • @Emily:您还在应该使用(...) 调用的地方使用[...] 订阅。 int() 是一个可调用对象,int("42") 将字符串 "42" 转换为值为 42 的整数,因为您调用了它并传入了该字符串。
    • 如果要根据数字获取月份名称,则需要订阅 MonthList,而不是相反。你确定你了解 Python 中数组和列表的工作原理吗?
    【解决方案3】:

    在一行

    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  
    

    【讨论】:

    • 其实我基本上也用过你的线,我只是做了一个单独的字段,将 NumOfMonth 定义为 int(NumOfMonth),非常感谢你的帮助!!!
    猜你喜欢
    • 2019-11-24
    • 2016-07-19
    • 2021-06-15
    • 2021-11-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-30
    • 2012-12-11
    相关资源
    最近更新 更多