0

Beginner here! Been racking my brain over this Python test question for two days...Can anyone point me in the right direction? Thanks :)

Tried about a dozen versions, but here's where I feel I'm closest:

=======================

def exam_grade(score):
    if exam_grade(score) >= 96:
        grade = "Top Score"
    elif exam_grade(score) >= 60 and <=95:
        grade = "Pass"
    else:
        grade = "Fail"
    return grade


print(exam_grade(65)) # Should print Pass
print(exam_grade(55)) # Should print Fail
print(exam_grade(60)) # Should print Pass
print(exam_grade(95)) # Should print Pass
print(exam_grade(100)) # Should print Top Score
print(exam_grade(0)) # Should print Fail 

=======================

*It seems like no matter what I tweak, I get syntax errors on >, <, <=, >=, or some end line character. :/

Randilyn
  • 1
  • 1
  • This doesn't work in python `exam_grade(score) >= 60 and <=95` you need `exam_grade(score) >= 60 and exam_grade(score) <=95` or similar. See also: https://stackoverflow.com/questions/20002503/why-does-a-x-or-y-or-z-always-evaluate-to-true-how-can-i-compare-a-to-al – Mark Feb 06 '23 at 19:04
  • The problem is not the `elif`, the problem is that `and` doesn't work that way. You need `elif grade >= 60 and grade <= 95:` or `elif 60 <= grade <= 95:`. BTW, don't call `exam_grade` recursively; inside the function, just use `grade`. – Tim Roberts Feb 06 '23 at 19:05
  • @TimRoberts `score`, not `grade` :) – chepner Feb 06 '23 at 19:08
  • why is the recursive call inside the func? like `elif exam_grade(...)`? It make no sense to me. remove the recursive call with `exam_grade` - this is not good use case for it. – rv.kvetch Feb 06 '23 at 19:09
  • score is the num, so type annotate for clarity: `score: float | int`. you don't need to recurse into func at all. you got the equality check vastly down, just need to tweak it a bit is all. – rv.kvetch Feb 06 '23 at 19:10
  • 1
    FWIW @rv.kvetch you don't need `float | int` — `float` is enough as [PEP 484](https://peps.python.org/pep-0484/#the-numeric-tower) points out. See also [this thread](https://stackoverflow.com/questions/50928592/mypy-type-hint-unionfloat-int-is-there-a-number-type) – Mark Feb 06 '23 at 19:28
  • 1
    @Mark Interesting - now I'll have to say TIL. Thanks for sharing ;) – rv.kvetch Feb 06 '23 at 20:10
  • Appreciate the help everyone :) – Randilyn Feb 17 '23 at 18:41

0 Answers0