Python Forum
TypeError: string indices must be integers, not str
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
TypeError: string indices must be integers, not str
#1
Hi,

I have a piece of code with the following type error:
Error:
TypeError: string indices must be integers, not str
I am new to Python and got stuck in my code. Would you please help me figure out whats the problem?
Many thanks



alice = {
    "name": "Alice",
    "homework": [100.0, 92.0, 98.0, 100.0],
    "quizzes": [82.0, 83.0, 91.0],
    "tests": [89.0, 97.0]
}
tyler = {
    "name": "Tyler",
    "homework": [0.0, 87.0, 75.0, 22.0],
    "quizzes": [0.0, 75.0, 78.0],
    "tests": [100.0, 100.0]
}
def get_average(student):
    homework = 0
    quizzes = 0
    tests = 0
    for key in student:
        homework =sum(key["homework"])/len(key["homework"])
        quizzes = sum(key["quizzes"])/len(key["quizzes"])
        tests = sum(key["tests"])/len(key["tests"])
    res = 0.1 * homework + 0.3 * quizzes + 0.6 * tests 
    print key["name"], float(res)
    
student = [alice,tyler]
get_average(student)
Reply


Messages In This Thread
TypeError: string indices must be integers, not str - by hanieh - Jan-06-2017, 05:08 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  TypeError: string indices must be integers thecoziest 3 6,234 Sep-29-2019, 05:34 PM
Last Post: ichabod801
  please Help. TypeError: list indices must be integers, not str skamaaa01 1 4,391 Feb-04-2018, 12:33 PM
Last Post: Gribouillis
  create a 20 digit string, and cast to a list then add all the digits as integers nikhilkumar 2 6,376 Jul-19-2017, 04:53 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020