Sahib Navlani
Sahib Navlani

Reputation: 80

Getting a unexpected error in Python

Traceback (most recent call last):
  File "C:\Users\sahib navlani\Desktop\gfh.py", line 107, in <module>
    main()
  File "C:\Users\sahib navlani\Desktop\gfh.py", line 98, in main
    crit2.play()
  File "C:\Users\sahib navlani\Desktop\gfh.py", line 34, in play
    self.play -= play1
TypeError: unsupported operand type(s) for -=: 'instancemethod' and 'int'

I get this error whenever i put this code . I think this due to line self.play -= play

play1 = int(raw_input("Please enter the time for which you want to play = "))
self.play -= play1

Upvotes: 0

Views: 64

Answers (2)

rmunn
rmunn

Reputation: 36718

I'm only guessing since you haven't shown us the code that's going wrong, but I think you're using the function name as a return value. This is a bad habit that Visual Basic teaches people -- nearly every other language uses return instead, which is what Python does. In Python, you would use a variable (I like to use result) to calculate what you'll return, then put return result at the end of your function. E.g.,

def addTwoNumbers(one, two):
    # addTwoNumbers = one + two   # Wrong!
    result = one + two
    return result

In a simple function like this, you could just write return one + two, but in more complicated functions, it's useful to use a variable to calculate the result, then return it at the end. So that's what I showed in this example.

Upvotes: 0

Mangu Singh Rajpurohit
Mangu Singh Rajpurohit

Reputation: 11420

It's because self.play is a member method. I think you have done mixing of names of member methods and member names. Give proper names to variables and be clear about the purpose for which you are using it.

Upvotes: 1

Related Questions