'Calculate GPA score in two decimals from multiple grade arguments
Need help to validate this code- something is not working in code. I have put this to calculate GPA from multiple arguments
\\
import sys
(sys.argv[0])
grades= (sys.argv[1])
def cal(grades):
i = 0
score = 0
x = {'A':4.0, 'A-':3.66, 'B+':3.33, 'B':3.0, \
'B-':2.66, 'C+':2.33, 'C':2.0, 'C-':1.66, \
'D+':1.33, 'D':1.00, 'D-':.66, 'F':0.00}
if grades !=[]:
for a in grades:
score += x[a]
gpa = score/len(grades)
return gpa
print(gpa)
else:
return None
print(None)
cal(grades)
\\
Solution 1:[1]
no value is getting printed because the return statements will return the value before it will actually be able to print anything.
you must print before returning the values Example:
def example(arg1):
return None,arg1
print("Result:",(None,arg1))
example(1)
it wouldn't show anything.
but if you do:
def functioningexample(arg1):
print("RESULT:",(None,arg1))
return None,arg1
fe = functioningexample(1)
print("it worked? the result Was :",fe)
you would get
>>> RESULT:(None,1)
>>> it worked? the result Was : (None,1)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
