简体   繁体   中英

Why do I keep getting a 'string indices must be integers' error?

I'm new to Python and I was trying to expand on a coding exercise from Codecademy. I want the program to take user input for the average of the selected student, but I keep getting a 'string indices must be integers' error.

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]
}

def average(numbers):
  total = sum(numbers)
  total = float(total)/len(numbers)
  return total

#List of Students to choose from for get_average input()
x = '1. Alice'
y = '2. Tyler'
z = '3. Lloyd'
#get_average function
def get_average(alice): 
    homework = average(alice['homework'])
    quizzes = average(alice['quizzes']) 
    tests = average(alice['tests'])
    student = input('Please type the number of the student who\'s weighted average you would like to see:' + '\n' + x + '\n' + y + '\n' + z)
    if student == '1':
      print( 0.1 * average(alice['homework']) + 0.3 * average(alice['quizzes']) + 0.6 * average(alice['tests']) )

get_average('student')

I was going to use the code for Alice's average to create a function for Lloyd's and Tyler's average, but I just can't figure the first part out. Can someone help me? Thanks.

Took the liberty to make some changes. First of all: You can put your data inside an array with dictionaries. The names are inside the array data under the key name.

We will use two functions from standard libraries. Dedent from textwrap to format text properly and mean from statistics to calculate mean.

data = [{
  "name": "Lloyd",
  "homework": [90.0, 97.0, 75.0, 92.0],
  "quizzes": [88.0, 40.0, 94.0],
  "tests": [75.0, 90.0]
    },
  {
  "name": "Alice",
  "homework": [100.0, 92.0, 98.0, 100.0],
  "quizzes": [82.0, 83.0, 91.0],
  "tests": [89.0, 97.0]
  },
    {
  "name": "Tyler",
  "homework": [0.0, 87.0, 75.0, 22.0],
  "quizzes": [0.0, 75.0, 78.0],
  "tests": [100.0, 100.0]
}]

And

from textwrap import dedent
from statistics import mean

def return_average(d):
    return 0.1 * mean(d['homework']) + 0.3 * mean(d['quizzes']) + 0.6 * mean(d['tests'])

def get_average(ar):
    # Create a list with all names
    # The whitespace will be removed with dedent
    namestring = '\n'.join(['            {}. {}'.format(idx, i['name']) 
                            for idx,i in enumerate(ar,1)])
    # Ask for a valid input
    while True:
        student = input(dedent('''\
            Please type the number of the student who's\ 
            weighted average you would like to see:\n{}\n'''.format(namestring)))

        if student in map(str,range(1,len(ar))):
            break
        else:
            print("Not valid input")

    # Send the valid input to return_average function 
    print(return_average(data[int(student)]))

get_average(data)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM