Home > Mobile >  how to rectify error - unsupported operand in python
how to rectify error - unsupported operand in python

Time:01-07

Traceback (most recent call last):
  File "C:\Users\Admin\Desktop\Python\proj.py", line 10, in <module>
    average = total/5
              ~~~~~^~
TypeError: unsupported operand type(s) for /: 'list' and 'int'

my program

language = [68, 46, 75, 85, 95 ]
english = [77, 84, 65, 90, 85]
math = [65, 55, 68, 87,98]
science = [78, 54, 87, 87, 89]
social = [76, 78, 86, 98, 89]
total = language   English   math   science   social
average = total/5
print ( "Average", average)

answers to solve this? I am a beginner for python learning.

CodePudding user response:

In the code you posted, the variable total is the result of concatenating five lists together. This is why you are getting the error "unsupported operand type(s) for /: 'list' and 'int'".

To fix the error, you need to add up the elements of the lists and store the result in a variable, and then divide that variable by the integer 5. You can do this using a loop or by using the built-in sum() function.

For example:

language = [68, 46, 75, 85, 95 ]
english = [77, 84, 65, 90, 85]
math = [65, 55, 68, 87,98]
science = [78, 54, 87, 87, 89]
social = [76, 78, 86, 98, 89]

total_sum = sum(language)   sum(english)   sum(math)   sum(science)   sum(social)
average = total_sum / 5
print("Average", average)

Alternatively, you could use a loop to iterate over the elements of the lists and add them up manually.

  • Related