0
"string indices must be integers error".python
Hello all! I'm trying to write a simple program that takes the name of my students, their scores and have the program automatically assign appropriate grades. I keep getting a "string indices must be integers error". I'm new at programming, please help!
12 ответов
+ 2
Do you have the code so i may see it?
+ 2
When you have questions like these one please always mention the programming language the problem occured. :)
+ 2
The bad idea is to use eval() function with the user input, instea casting it directly to integer, as explained by @Amarie, because eval() will return a float, and almost is unsafe, as if user enter valid Python code, he could do anything unexpected ^^
0
total_num_students=0
students=eval (input ('how many students? '))
while total_num_students <students:
student_name=input ('student name: ')
midterm=eval (input ('midterm scores : '))
exam=eval (input ('exam score: "))
final_score=((exam/50)*70)+midterm
total_num_students+=1
for i,j,k,l in student_name,midterm,exam,final_score:
print (student_name [i],midterm [j],exam [k],final_score[l])
0
Please ignore the indentation
0
so python correct?
0
Okay... to answer your question:
This error occurs, when you try to get an element of a string with a float number. For example:
mystring = "abcde"
print (mystring[1.0])
to solve this you have to convert the float to an integer first. This could be done by the int () funktion:
a = 1.0
b = int (a)
0
yeah so to make sure your code is reading right i would do either float or int surrounding your input value so it doesn't string
0
Lastly if you take the students and put them in to an array and have everything else put into a seperate array so that if you call x it bring up x student
0
Thanks guys. much appreciated
0
your welcome. if you ever need anymore help just ask
0
yeah i agree with visph because it is so much easier with int()