简体   繁体   English

typeerror字符串索引必须是整数,而不是str python

[英]typeerror string indices must be integers not str python

I have defined a function get_class_average() having one argument "students". 我定义了一个具有一个参数“ students”的函数get_class_average() students is a list of 3 students: lloyd, alice, tyler, which are indirectly dictionaries. students是3个学生的列表:lloyd,alice,tyler,它们是间接词典。 get_average() is a function that calculates average and it works fine. get_average()是一个计算平均值的函数,它可以正常工作。 When I try to call the function get_class_average() I'm getting error: 当我尝试调用函数get_class_average()出现错误:

typeerror string indices must be integers not str

My code 我的密码

lloyd = {
    "name": "Lloyd",
    "homework": [90.0, 97.0, 75.0, 92.0],
    "quizzes": [88.0, 40.0, 94.0],
    "tests": [75.0, 90.0]
}
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]
}

# Add your function below!
from math import *
def average(numbers):
    total = sum(numbers)
    total = float(total)
    ans = total / len(numbers)
    return ans
num = [1,2,4,5,6]
a = average(num)
print a

def get_average(student):
    homework = average(student["homework"])
    quizzes = average(student["quizzes"])
    tests = average(student["tests"])
    sum = (0.1*homework) + (0.3 * quizzes) + (0.6 * tests)
    return sum

student = [lloyd, alice, tyler]
for key in student:   
    a = get_average(key)
    print a

def get_letter_grade(score):
    if score >= 90:
        return "A"
    elif score >= 80:
        return "B"
    elif score >= 70:
        return "C"
    elif score >= 60:
        return "D"
    else:
        return "F"

avg = get_average(lloyd)
t = get_letter_grade(avg)
print t


def get_class_average(students):
    results = []
    students = [lloyd, alice, tyler]
    a= get_average(students)
    r= results.append(a)
    return r

student = [lloyd, alice, tyler]
for key in student:   
    a = get_class_average(key)

print a

Your get_class_average() function expects to be passed the whole list of students, not each individual student. 您的get_class_average()函数希望传递给整个学生列表,而不是每个学生。

Remove your for key in student: loop: 删除for key in student:for key in student:循环:

students = [lloyd, alice, tyler]
a = get_class_average(students)

Within the get_class_average() function you appear to be returning too early ; get_class_average()函数中,您似乎返回得太早了 unindent the return to not be part of the loop. 使return不缩进不成为循环的一部分。 results.append() returns None as it alters results in-place : results.append()返回None ,因为它改变了results 就地

def get_class_average(students):
    results = []
    for key in students:   
        a= get_average(key)
        results.append(a)
    return results

This still doesn't calculate the average for the whole class, however; 但是,这仍然不能计算出整个班级的平均水平。 it just collects the averages for each student into a list. 它只是将每个学生的平均值收集到一个列表中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 TypeError:字符串索引必须是整数,而不是python中的str - TypeError: string indices must be integers, not str in python Python、Flask - 类型错误:字符串索引索引必须是整数或切片,而不是字符串 - Python, Flask - TypeError: string index indices must be integers or slices, not str Python和网络X中的错误-TypeError:字符串索引必须是整数,而不是str - Error in Python and network X - TypeError: string indices must be integers, not str TypeError:字符串索引必须是整数,而不是Python字典上的str - TypeError: string indices must be integers, not str on Python Dictionary Scrapy:TypeError:字符串索引必须是整数,而不是str? - Scrapy: TypeError: string indices must be integers, not str? TypeError:字符串索引必须是整数,而不是str-OpenErp - TypeError: string indices must be integers, not str - OpenErp zeromq:TypeError:字符串索引必须是整数,而不是str - zeromq: TypeError: string indices must be integers, not str TypeError:字符串索引必须是整数,而不是Pt []中的str - TypeError: string indices must be integers, not str in Pt[] TypeError:字符串索引必须是整数,而不是str 4 - TypeError: string indices must be integers, not str 4 'TypeError:列表索引必须是整数,而不是str'Python 3 - 'TypeError: list indices must be integers, not str' Python 3
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM