fork download
  1. def grades(*args):
  2.  
  3. # this checks for invalid grades
  4. invalid = [str(y) for y in args if not (0 <= y <= 100)]
  5. if invalid:
  6. raise ValueError('invalid grades entered: ' + ', '.join(invalid))
  7.  
  8. # puts passing grades in a list
  9. passing = [x for x in args if x >= 50]
  10.  
  11. # return 0.0 if no passing grades are found; else average out the rest
  12. return 0.0 if not passing else sum(passing)/len(passing)
  13.  
  14. print(grades(10, 30)) # 0.0
  15. print(grades(60, 30)) # 60.0
  16. print(grades(60, 80)) # 70.0
  17. print(grades(70, 80, 90)) # 80.0
  18.  
  19. print(grades(100, 120)) # raises error
Runtime error #stdin #stdout #stderr 0.03s 9440KB
stdin
Standard input is empty
stdout
0.0
60.0
70.0
80.0
stderr
Traceback (most recent call last):
  File "./prog.py", line 19, in <module>
  File "./prog.py", line 6, in grades
ValueError: invalid grades entered: 120