1

I have this simple python program involving entering a given mark for a subject and then seeing what range is falls under in order to return the appropriate grade


# ask user for the subject code 
user_input = input("Enter subject code: ")
subject_code = user_input

# ask user for the mark 
user_input = input("Enter mark: ")
subject_mark = int(user_input)

print("RESULT: ")
print("Subject code: " + subject_code)
print("Mark: " + str(subject_mark))

if subject_mark <100 and subject_mark >85:
  print("Grade: HD")
elif subject_mark <84 and subject_mark >75:
  print("Grade: D")
elif subject_mark <74 and subject_mark >65:
  print("Grade: C")
elif subject_mark <64 and subject_mark >50:
  print("Grade: P")
elif subject_mark <49 and subject_mark >0:
  print("Grade: F")
else:
    print("invalid")

however, when i run this program it doesnt print the Grade. It only prints this

Enter subject code: MATH111
Enter mark: 85
'RESULT: 
Subject code: MATH111
Mark: 85

Where as it should print this

Enter subject code: MATH111
Enter mark: 85
'RESULT: 
Subject code: MATH111
Mark: 85
Grade: HD

Any help is much appreciated

4
  • 2
    85 is neither greater than 85 nor less than 85. Perhaps you want to use something like <= or >= somewhere in your code. You are also being needlessly verbose. Later conditions are only tested if earlier conditions fail, so you don't need to test for the failure of earlier conditions when formulating later conditions. Commented Mar 9, 2021 at 11:12
  • 1
    If you look at your statement carefully, you'll notice that the if else statement is not handling the case where the mark is 85 at all. Same for 84, 75, and so on. Use >=. Commented Mar 9, 2021 at 11:15
  • 1
    @JohnColeman it should however print "invalid" Commented Mar 9, 2021 at 11:21
  • By the way, you can write if statements like so: if 85 <= subject_mark < 100: Commented Mar 9, 2021 at 11:27

2 Answers 2

1

You should use => instead of >.

if subject_mark <100 and subject_mark >=85:
  print("Grade: HD")
elif subject_mark <84 and subject_mark >=75:
  print("Grade: D")
elif subject_mark <74 and subject_mark >=65:
  print("Grade: C")
elif subject_mark <64 and subject_mark >=50:
  print("Grade: P")
elif subject_mark <49 and subject_mark >=0:
  print("Grade: F")
else:
    print("invalid")
Sign up to request clarification or add additional context in comments.

Comments

0

Use <= or >= instead of > or <

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.