Siddharth Sachar
Siddharth Sachar

Reputation: 1

Getting and error when making a domain in my python code

I need some help with my code. I made a basic grading system that looks liek this

grade = float(input("Enter the your grade:\n"))
if 81 < grade:
    if 98 <= grade <= 100:
        grade = "B+"
    if 91 <= grade <= 97:
        grade = "B"
    if 81 <= grade <= 90:
        grade = "B-"
if 71 < grade <= 80:
    if 78 <= grade <= 80:
        grade = "B+"
    if 74 <= grade <= 77:
        grade = "B"
    if 71 <= grade <= 73:
        grade = "B-"
if 61 < grade <= 70 :
    if 68 <= grade <= 70:
        grade = "C+"
    if 64 <= grade <= 67:
        grade = "C"
    if 61 <= grade <= 63:
        grade = "C-"
if 51 < grade <= 60:
    if 58 <= grade <= 60:
        grade = "D+"
    if 54 <= grade <= 57:
        grade = "D"
    if 51 <= grade <= 53:
        grade = "D-"
if 50 > grade :
   grade = "F"

print(grade)

When I try to run it I get this message

Traceback (most recent call last):
  File "main.py", line 2, in <module>
    if 81 < grade:
TypeError: '<' not supported between instances of 'int' and 'str'

Can someone figure out what I'm doing wrong because I have zero clue XD

Upvotes: 0

Views: 51

Answers (4)

BLimitless
BLimitless

Reputation: 2605

Your challenge is that by using if statements everywhere, after your grade gets assigned a letter, 'B+' for example, then all the other if statements still evaluate and you're now comparing a string to an int.

You can use the elif framing to avoid errors like this, and also simplify, shorten, and de-nest your code. The way the if, elif, and else construct works is python will evaluate the if, if that's False, then it moves through the elifs until it finds an elif that evaluates to True. Then it executes the code block in that one elif and stops.

If none of the elif statements evaluate to True, then it executes whatever is in the else block:

grade = float(input("Enter the your grade:\n"))
if 98 <= grade <= 100:
    grade = "A+"
elif 91 <= grade < 98:
    grade = "A"
elif 81 <= grade < 91:
    grade = "A-"
elif 78 <= grade < 81:
    grade = "B+"
elif 74 <= grade < 78:
    grade = "B"
elif 71 <= grade < 74:
    grade = "B-"
elif 68 <= grade < 71:
    grade = "C+"
elif 64 <= grade < 68:
    grade = "C"
elif 61 <= grade < 64:
    grade = "C-"
elif 58 <= grade < 61:
    grade = "D+"
elif 54 <= grade < 58:
    grade = "D"
elif 51 <= grade < 54:
    grade = "D-"
else:
    grade = "F"

print(grade)

Lastly, I replaced your double 'B' grades with 'A', so now a 98 is an A+ instead of a B+.

Upvotes: 0

Ben Y
Ben Y

Reputation: 1023

You're replacing the float value with a string. I think each of your if statements (after the first one) should be changed to elif statements so it doesn't keep getting tested.

This changes the code completely, but I decided to store the lower bounds and assume grade can never be above 100.

# Appears like it's impossible to get any A grades, but we have double B ranges.
gr_table = [
    (98, 'B+'), (91, 'B'), (81, 'B-'), (78, 'B+'), (74, 'B'), (71, 'B-'),
    (68, 'C+'), (64, 'C'), (61, 'C-'), (58, 'D+'), (54, 'D'), (51, 'D-'),
    (0, 'F')
]

grade = float(input("Enter the your grade:\n"))
for lim, val in gr_table:
    if grade >= lim:
        grade = val
        break

Upvotes: 0

Zichzheng
Zichzheng

Reputation: 1290

You code logic is right, however, you should make this as a function and return the answer. Here:

if 81 < grade:
if 98 <= grade <= 100:
    grade = "B+"
if 91 <= grade <= 97:
    grade = "B"
if 81 <= grade <= 90:
    grade = "B-"

If we say the grade is 95, and after the if 91 <= grade <= 97: it will become "B" and then, when you do if 81 <= grade <= 90:, it's going to clash. You can make this a method and use return or you could use another variable to store the result like:

grade = float(input("Enter the your grade:\n"))
result = ''
if 81 < grade:
    if 98 <= grade <= 100:
        result = "B+"
    if 91 <= grade <= 97:
        result = "B"
    if 81 <= grade <= 90:
        result = "B-"
if 71 < grade <= 80:
    if 78 <= grade <= 80:
        result = "B+"
    if 74 <= grade <= 77:
        result = "B"
    if 71 <= grade <= 73:
        result = "B-"
if 61 < grade <= 70 :
    if 68 <= grade <= 70:
        result = "C+"
    if 64 <= grade <= 67:
        result = "C"
    if 61 <= grade <= 63:
        result = "C-"
if 51 < grade <= 60:
    if 58 <= grade <= 60:
        result = "D+"
    if 54 <= grade <= 57:
        result = "D"
    if 51 <= grade <= 53:
        result = "D-"
if 50 > grade :
   result = "F"

print(result)

Upvotes: 0

enzo
enzo

Reputation: 11506

Basically, in the first line you're assign grade to a float value. Then, you compare this float value with a int value (which is OK). When a condition matches, you assign grade to a string value and compare this string value with a int value. Since you can't do that, the error is raised. There are two solutions:

  • Use elifs:
grade = float(input("Enter the your grade:\n"))
if 81 < grade:
    if 98 <= grade <= 100:
        grade = "B+"
    elif 91 <= grade <= 97:
        grade = "B"
    elif 81 <= grade <= 90:
        grade = "B-"
elif 71 < grade <= 80:
    if 78 <= grade <= 80:
        grade = "B+"
    elif 74 <= grade <= 77:
        grade = "B"
    elif 71 <= grade <= 73:
        grade = "B-"
elif 61 < grade <= 70 :
    if 68 <= grade <= 70:
        grade = "C+"
    elif 64 <= grade <= 67:
        grade = "C"
    elif 61 <= grade <= 63:
        grade = "C-"
elif 51 < grade <= 60:
    if 58 <= grade <= 60:
        grade = "D+"
    elif 54 <= grade <= 57:
        grade = "D"
    elif 51 <= grade <= 53:
        grade = "D-"
elif 50 > grade :
   grade = "F"
  • Use a new variable:
grade = float(input("Enter the your grade:\n"))
if 81 < grade:
    if 98 <= grade <= 100:
        grade_str = "B+"
    if 91 <= grade <= 97:
        grade_str = "B"
    if 81 <= grade <= 90:
        grade_str = "B-"
if 71 < grade <= 80:
    if 78 <= grade <= 80:
        grade_str = "B+"
    if 74 <= grade <= 77:
        grade_str = "B"
    if 71 <= grade <= 73:
        grade_str = "B-"
if 61 < grade <= 70 :
    if 68 <= grade <= 70:
        grade_str = "C+"
    if 64 <= grade <= 67:
        grade_str = "C"
    if 61 <= grade <= 63:
        grade_str = "C-"
if 51 < grade <= 60:
    if 58 <= grade <= 60:
        grade_str = "D+"
    if 54 <= grade <= 57:
        grade_str = "D"
    if 51 <= grade <= 53:
        grade_str = "D-"
if 50 > grade :
   grade_str = "F"

The best solution is to use both solutions above.

Upvotes: 1

Related Questions