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!

16th Jun 2017, 5:58 PM
Mohammed Issifu
Mohammed Issifu - avatar
12 Antworten
+ 2
Do you have the code so i may see it?
16th Jun 2017, 6:02 PM
NulledNVoided
NulledNVoided - avatar
+ 2
When you have questions like these one please always mention the programming language the problem occured. :)
16th Jun 2017, 6:03 PM
Amarie
+ 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 ^^
16th Jun 2017, 8:06 PM
visph
visph - avatar
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])
16th Jun 2017, 6:14 PM
Mohammed Issifu
Mohammed Issifu - avatar
0
Please ignore the indentation
16th Jun 2017, 6:15 PM
Mohammed Issifu
Mohammed Issifu - avatar
0
so python correct?
16th Jun 2017, 6:15 PM
NulledNVoided
NulledNVoided - avatar
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)
16th Jun 2017, 6:17 PM
Amarie
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
16th Jun 2017, 6:20 PM
NulledNVoided
NulledNVoided - avatar
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
16th Jun 2017, 6:22 PM
NulledNVoided
NulledNVoided - avatar
0
Thanks guys. much appreciated
16th Jun 2017, 6:31 PM
Mohammed Issifu
Mohammed Issifu - avatar
0
your welcome. if you ever need anymore help just ask
16th Jun 2017, 6:35 PM
NulledNVoided
NulledNVoided - avatar
0
yeah i agree with visph because it is so much easier with int()
16th Jun 2017, 8:21 PM
NulledNVoided
NulledNVoided - avatar