-2

I'm trying to create a program which calculates overall scores and outputs a grading system

Say Pass, Merit, Distinction depending on which grade they got.

I've tried the code below but it's no use. Also tried using and/or statements but no luck.

number = input("Enter a number: ")

if number < 40: 
    print("Failed")

elif number > 40 < 50:
    print("Pass")

elif number > 50 < 60:
    print("Merit")

else:
    print("Distinction")

So I'm basically trying to get this to work, however the conditional statements aren't working and can't properly categorise the mark.

Any help?

AJD98
  • 103
  • 9

3 Answers3

1
number = int(input("Enter a number: "))

if number < 40: 
    print("Failed")
elif number < 50:
    print("Pass")
elif number < 60:
    print("Merit")
else:
    print("Distinction")

The conditions are chained. If you ever reach elif number < 50, that means the previous if number < 40 must have been False (i.e. the number is >= 40). You do not need to retest this condition again.

The code does: "Is it under 40? No? Is it under 50 then? No? Is it under 60 then? No? Wow, it's over 60 then!" Or if it is, say, under 50, that's where the comparisons stop.

For the sake of completeness, the correct way to write a chained comparison would be:

if 40 < number < 50

This tests whether the number is between 40 and 50.

deceze
  • 510,633
  • 85
  • 743
  • 889
0

The return value of input is of type str, so to compare it with numbers, it should be first converted to type int

number = 0
try:
    number = int(input("Enter a number:"))
except:
    print("Please input an integer")
    exit(1)

# Now write your conditions     
if number > 100 or number < 0:
    print("Sorry grades can only be between 0 and 100")
elif number < 40:
    print("Failed")
elif number < 50:
    print("Pass")
elif number< 60:
    print("Merit")
else:
    print("Distinction")
Sriteja Sugoor
  • 574
  • 3
  • 13
-1

If I understood correctly your conditions, the code should be like the following one:

number = input("Enter a number: ")

if number < 40: 
    print("Failed")

elif number >= 40 and number < 50:
    print("Pass")

elif number >= 50 and  number< 60:
    print("Merit")

else:
    print("Distinction")
Giordano
  • 5,422
  • 3
  • 33
  • 49
  • You can omit the redundant >= test. – deceze Jan 06 '19 at 10:51
  • Probably i miss the point, but why? do you mean that i can remove >=40 and >=50? – Giordano Jan 06 '19 at 10:54
  • Yes. Because you’ve already ruled out that condition by the previously chained `if` statement. It wouldn’t go into the `elif` if the number was smaller than 40. – deceze Jan 06 '19 at 10:55
  • Ok, but what happen if user inserts 50? the code will print Distinction, but the correct result is Merit right? – Giordano Jan 06 '19 at 11:02
  • Why would it print “Distinction”? – deceze Jan 06 '19 at 11:03
  • If i remove all equals symbol from the code, number = 50 is not included in any if condition, but only in the else branch... – Giordano Jan 06 '19 at 11:06
  • `if number < 40: ... elif number < 50: ...`. When you enter 39, the first condition applies, when you enter 40 it falls through to the next `elif`. You don't have to *retest* the (reverse of the) previous condition. – deceze Jan 06 '19 at 11:11