-1

I am creating a Marks sheet for school which code is below:

Sub1 = input ('Enter Marks of English: ')
Sub2 = input ('Enter Marks of Urdu: ')
Sub3 = input ('Enter Marks of Science: ')
Sub4 = input ('Enter Marks of Maths: ')
Sub5 = input ('Enter Marks of Islamiat: ')
avg = (Sub1+Sub2+Sub3+Sub4+Sub5)
Sub1 = int(Sub1)
Sub2 = int(Sub2)
Sub3 = int(Sub3)
Sub4 = int(Sub4)
Sub5 = int(Sub5)
if avg >90 or (avg <80):
    print("Grade: A")
elif avg > 79 or (avg < 61):
    print("Grade: B")
elif avg > 60 or (avg < 40):
    print("Grade: C")
elif avg > 40 or (avg < 39):
    print("Grade: D")
else:
    print("Grade: F")

However getting error when taking average as follow

TypeError
Traceback (most recent call last)
<ipython-input-25-68aee6e8df0f> in <module>
     12 Sub5 = int(Sub5)
     13
---> 14 if avg >90 or (avg <80):
     15     print("Grade: A")
     16 elif avg > 79 or (avg < 61):
TypeError: '>' not supported between instances of 'str' and 'int'

Please help to fix tomorrow have to submit my assignment.

kaya3
  • 47,440
  • 4
  • 68
  • 97
  • Do the `avg()` after you `int`ed the inputs (which are strings). And also, you probably want to divide it by 5 to get the average and not the sum. – Aryerez Nov 21 '19 at 18:32
  • 1
    I think your logic is a bit wonky. You say a person should get an A if their score is above 90 or less than 80 -- so a score of 4 is an A? – SyntaxVoid Nov 21 '19 at 18:40

2 Answers2

1

When you read user input your variables are str. So, you need to conver your Subn variables to int before adding. Replace:

avg = (Sub1+Sub2+Sub3+Sub4+Sub5)
Sub1 = int(Sub1)
Sub2 = int(Sub2)
Sub3 = int(Sub3)
Sub4 = int(Sub4)
Sub5 = int(Sub5)

for

Sub1 = int(Sub1)
Sub2 = int(Sub2)
Sub3 = int(Sub3)
Sub4 = int(Sub4)
Sub5 = int(Sub5)
avg = (Sub1+Sub2+Sub3+Sub4+Sub5)
Croves
  • 400
  • 3
  • 11
  • you did it man :-) only one line give a lot issues, appreciated your help thanks alot – Abid Hussain Nov 21 '19 at 18:32
  • In python, if you add two strings it will join them. For example, if you have first_name = 'Abid' and last_name 'Hussain', first_name + last_name will returns 'AbidHussain'. So, even if your user input is a number, python will assume it's a string. That's why you need to convert for INT before doing the math. Also, be aware that you are not calculating an average; you must divide by 5 then – Croves Nov 21 '19 at 18:39
0

You have code to convert the inputs from strings to ints, which is necessary to avoid a TypeError here. But you are calculating avg before this conversion, so avg is a string. The solution is to move the line

avg = (Sub1+Sub2+Sub3+Sub4+Sub5)

to after you have converted these variables into ints.

Note also that this formula gives the sum, not the average.

kaya3
  • 47,440
  • 4
  • 68
  • 97