Getting the name of the student as an input - Error - Help!


#1

https://www.codecademy.com/courses/learn-python/lessons/student-becomes-the-teacher/exercises/sending-a-letter?action=lesson_resume&link_content_target=interstitial_lesson

Enter the name: alice
Traceback (most recent call last):
File “python”, line 45, in
File “python”, line 27, in get_average
TypeError: string indices must be integers, not str

<What do you expect to happen instead?>
I expect to get the input from the user and print the corresponding grade of the student. But I get error. Please help.


# Add your function below!
def average(numbers):
  total = sum(numbers)
  total = float(total)
  return total/len(numbers)

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

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"
x = raw_input("Enter the name: ")
print get_letter_grade(get_average(x))



#2

Hi @charlesbronsonlt,

What you are attempting to do goes beyond what the exercise instructions ask. Codecademy’s submission correctness test (SCT) for an exercise will often, but not always, reject submissions that depart from the instructions.

In this case, however, your code raises an actual Python runtime error. The get_average function is designed to take, as an argument, the dictionary that represents one of the students. Instead, you are passing it the string that the user enters. To succeed at what you are trying to do, you need to have your code look through the dictionaries to find the one that contains the name that the user entered, for example, "Lloyd", and then pass that dictionary to the get_average function.


#3

Isn’t my string(the input I get from user) the name of the dictionary? So when the user enters “lloyd”. Doesn’t it mean the get_average(lloyd) which takes it to the get_average() function where the argument student is replaced by lloyd and the function is run.

If not, explain me what is the difference between getting a string as input and directly entering the name of the dictionary?

Also, if possible please explain me how to get the input and do the same process.
Thank you for your detailed explanation


#4

Yes, but you are passing it to the function as a string, when the function needs to receive it as a variable that represents a student dictionary.

It is best not to depend on the user to know the names of variables in a program. Instead, ask the user to enter the name of the student, and have the program find the dictionary that contains the name. Try the following as an example …

students = [lloyd, alice, tyler]
# Get the name of the student.
x = raw_input("Enter the name: ")
# Find the dictionary that contains that name.
for student in students:
  if student["name"] == x:
    # Name found; display the grade.
    print get_letter_grade(get_average(student))
    break
else:
  # Name not found.
  print "Could not find that name."

#5

Yes, but you are passing it to the function as a string, when the function needs to receive it as a variable that represents a student dictionary.

I am a wee bit confused in here. Aren’t strings a type of variable?

It is best not to depend on the user to know the names of variables in a program. Instead, ask the user to enter the name of the student, and have the program find the dictionary that contains the name. Try the following as an example …

students = [lloyd, alice, tyler]
# Get the name of the student.
x = raw_input("Enter the name: ")
# Find the dictionary that contains that name.
for student in students:
  if student["name"] == x:
    # Name found; display the grade.
    print get_letter_grade(get_average(student))
    break
else:
  # Name not found.
  print "Could not find that name."

Yeah… This works well. Thanks for this. I’ll keep that in mind.
But doesn’t it add another 2 lines of code and also what is the difference between mine and this. Aren’t the function arguments strings in both cases?


#6

While a string might contain the name of a variable, it is not the same thing as the variable itself.

It does add some lines of code, but those lines are important in that they use the string representing a student’s name to find the dictionary that contains that name.


#7

I could not understand this highlighted part of the code. What is “name” in here? How is the value entered by the user checked?
I tried running it and it is showing “could not find that name”, whatever the name I type, even for the names that are already present (alice, tyler etc.,)


#8

This is the lloyd dictionary …

lloyd = {
    "name": "Lloyd",
    "homework": [90.0, 97.0, 75.0, 92.0],
    "quizzes": [88.0, 40.0, 94.0],
    "tests": [75.0, 90.0]
}

The following accesses the "name" item in the dictionary, which is this case is "Lloyd". …

  if student[“name”] == x:  

When you run the code, enter such a string as Lloyd, which is the name of a student, rather than the lowercase lloyd, which is the name of a variable.


#9

Enter the name: Lloyd
Traceback (most recent call last):
File “python”, line 56, in
TypeError: list indices must be integers, not str

I still don’t get the output. I got this error.


#10

Without our seeing all of the code, we cannot be sure of the nature of the problem.

For one thing, make sure to have this …

      if student["name"] == x:

… and not this …

      if students["name"] == x:

… in the for loop

The latter of those two could produce the TypeError that you reported.

If that is not the problem, copy and paste all of the code that you have for this exercise, so we can look at it.

EDITED October 14, 2017 to revise the example of postulated erroneous code above.


#11

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.