python - Why am I getting the error 'string indices must be integers' in this line of code? -
okay know doesn't tidy, on line 'finalmessage' getting error 'string indices must integers' , need little putting right , understanding why. appreciated :)
startbalance = input("enter 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") 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 see interest , balance - ") finalmessage = float(numofmonth[monthlist]), float(interest[monthlist]), float(totalbalance[monthlist]) print(finalmessage) response = "" while response != "yes" , response != "no": response = input("would see month? - ") if response == "yes": if response != "yes" , response != "no": print("invalid input, please enter yes or no") if response == "no": endofinput = true print("thank using program, goodbye :)")
you define monthlist
(heh) list here monthlist = []
. try use index here numofmonth[monthlist]
, predictably fails.
i assume wanted xth month, translate into:
monthlist[numofmonth]
but have wrong indexation here interest[monthlist]
, again, assume , meant interestlist[numofmonth]
edit
as pointed out in comments, you'd have convert numofmonth int first numofmonth=int(numofmonth)
Comments
Post a Comment