I am a new coder and have seen people on Stack discuss why eval use is not recommended. In the following code, we have been given a set of dictionaries with the marks of the students, we take the student name as an input and provide the output as per the formula given (the formula for the calculation of marks).
# 1. Jack's dictionary
Jack = { "name":"Jack",
"assignment" : [80, 50, 40, 20],
"test" : [75, 75],
"lab" : [78.20, 77.20]
}
# 2. James's dictionary
james = { "name":"James",
"assignment" : [82, 56, 44, 30],
"test" : [80, 80],
"lab" : [67.90, 78.72]
}
# 3. Dylan's dictionary
dylan = { "name" : "Dylan",
"assignment" : [77, 82, 23, 39],
"test" : [78, 77],
"lab" : [80, 80]
}
# 4. Jessica's dictionary
jess = { "name" : "Jessica",
"assignment" : [67, 55, 77, 21],
"test" : [40, 50],
"lab" : [69, 44.56]
}
# 5. Tom's dictionary
tom = { "name" : "Tom",
"assignment" : [29, 89, 60, 56],
"test" : [65, 56],
"lab" : [50, 40.6]
}
name = eval(input())
marks = (0.1*(sum(name['assignment'])/len(name['assignment']))) + (0.7*(sum(name['test'])/len(name['test']))) + (0.2*(sum(name['lab'])/len(name['lab'])))
asci = 65
print('Average marks of {} is :'.format(name['name']),marks)
def GradeAssign(a,marks,asci):
if a >= 60:
if marks >= a:
print('Letter Grade of {} is :'.format(name['name']),chr(asci))
else :
a -= 10
asci += 1
GradeAssign(a,marks,asci)
else :
print('Letter Grade of {} is :'.format(name['name']),'E')
GradeAssign(90,marks,asci)
Now I can't seem to figure out a way to not use eval in the following line:
name = eval(input())
name = input()
, and use that to access the values in the dictionary. \$\endgroup\$