scores = { 'C':'A', 'Java':'F', 'mobile':'C', 'security':'A ',
'hack':'F', 'python':'A', 'os':'A ' }
credit = { 'A ':'4.5', 'A':'4.0', 'B ':'3.5', 'B':'3.0', 'C ':'2.5',
'C':'2.0', 'D ':'1.5', 'D':'1.0', 'F':'0.0' }
for key in scores.keys():
print(key, '\t', scores[key])
sum = 0
for key in scores.keys():
average = sum(credit[scores[key]])
print('your average is :', average)
This is my code and I want to show it like:
C A
Java F
mobile C
security A
hack F
python A
os A
your average is : 2.71
but it has "unsupported operand type(s) for : 'int' and 'str'"
error but I can't fix it.
CodePudding user response:
I will try to walk you through the error and the code. The error tells you that Python can not have the operation
between a string and an integer. You can tell that this error is in the line inside the for loop. You can know that by the line number in the error message in your editor as well.
If you look at what you do in the for loop, you sum values from a dictionary credit
whose values are strings. That is what causes the error, so convert the values in credit
to integer. You can do this simply as follows:
credit = {key: float(credit[key]) for key in credit}
If you do so, you will have another error and it is something like integer is not collable
. This is because you defined sum
as zero before the for loop, and inside you try to use sum()
which does not make any sense here.
To compute the average, you would like to add the credit of each subject to the cumulative sum variable sum
that you initiated by zero, and at the end you divide by there number.
total_credit = 0
for key in scores.keys():
total_credit = total_credit credit[scores[key]]
average = total_credit / len(scores)
print('your average is :', average)
CodePudding user response:
scores = { 'C':'A', 'Java':'F', 'mobile':'C', 'security':'A ',
'hack':'F', 'python':'A', 'os':'A ' }
credit = { 'A ':'4.5', 'A':'4.0', 'B ':'3.5', 'B':'3.0', 'C ':'2.5',
'C':'2.0', 'D ':'1.5', 'D':'1.0', 'F':'0.0' }
# First, you have to transform the credits grades from string to floats
for i in credit:
credit[i] = float(credit[i])
# Print all keys,values
for key in scores.keys():
print(key, '\t', scores[key])
# Print average grade
print(
'your average is', sum( credit.values() ) / len(credit.values())
)
CodePudding user response:
You can use a simple generator passed to the built-in sum function as follows:
scores = { 'C':'A', 'Java':'F', 'mobile':'C', 'security':'A ',
'hack':'F', 'python':'A', 'os':'A ' }
credit = { 'A ':'4.5', 'A':'4.0', 'B ':'3.5', 'B':'3.0', 'C ':'2.5',
'C':'2.0', 'D ':'1.5', 'D':'1.0', 'F':'0.0' }
print(sum(float(credit[k]) for k in scores.values()) / len(scores))
CodePudding user response:
sum
is function, so use that as name of variable is no good idea.
What about somethink like that ?
scores = { 'C':'A', 'Java':'F', 'mobile':'C', 'security':'A ',
'hack':'F', 'python':'A', 'os':'A ' }
credit = { 'A ':'4.5', 'A':'4.0', 'B ':'3.5', 'B':'3.0', 'C ':'2.5',
'C':'2.0', 'D ':'1.5', 'D':'1.0', 'F':'0.0' }
for key in scores.keys():
print(key, '\t', scores[key])
lst_marks = [float(credit[scores[key]]) for key in scores.keys()]
avg = sum(lst_marks)/len(lst_marks)
print(f"your average is : {avg}")