0

I can't figure out why I get the error: TypeError: Can't convert 'int' object to string implicity

This is the relevant code for the class:

class Pig():

def __init__(self, name, age, weight, value):
    self.name = name
    self.age = age
    self.weight = weight
    self.value = value

def runAndGainValue(self, value):
    self.value += value  #This is where the error occur.

def __str__(self):
    a = self.name + " "
    a += str(self.value) + " "
    return a

And here's the part of the code for the main program:

elif work == "2":
    yourfarm.printAnimals()
    print ("blablablabla")
    for pig in p:
        pig.runAndGainValue(5)
    yourfarm.printAnimals()

And I can't figure out why I get this error. I've tried searching for it but I am new with programming so I have a lot of trouble with interpreting a totally different code but with the same problem. Very much grateful for your help ahead.

4
  • where does the error occur? Commented Nov 3, 2013 at 23:12
  • is self.name a string, or are you passing in a number when you create the Pig object? Commented Nov 3, 2013 at 23:13
  • Ops sry, I've edited the question and commented where the error occur. Commented Nov 3, 2013 at 23:14
  • I'm not sure what you mean because I'm a superbeginner. But the self.name is decided in my class for the farm. with for i in self.animals: print(i.name,i.weight, and so on). Commented Nov 3, 2013 at 23:15

1 Answer 1

2

You have set the value of your pig to a string, then when you call pig.runAndGainValue(5) you are trying to add an integer to a string:

def runAndGainValue(self, value):
    self.value += value

This raises an exception as Python strings are not implicitly converted to numbers, even when their value could be interpreted as a number:

>>> '10' + 5
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: Can't convert 'int' object to str implicitly

When creating the Pig, make sure value in an integer, always. Perhaps you need to explicitly convert in the initializer:

def __init__(self, name, age, weight, value):
    self.name = name
    self.age = int(age)
    self.weight = int(weight)
    self.value = int(value)

or just make sure your inputs are integers.

Sign up to request clarification or add additional context in comments.

7 Comments

I see. That brings me a new problem, because of how I've decided the age, weight, and value in my main code. I wrote it like Pig("bla the pig", "age: 2", "weight: 50" if you understand what I mean.
@Bondenn: You cannot do that; let the attribute names themselves determine what the value means, don't store strings with added metadata like that.
@Bondenn: At least, parse out the value: self.age = int(age.rsplit(':', 1)[-1]) would get just the part of the string after the last : and turn that into an integer.
Okey that's my bad for being a beginner! But how do I do it when I want the attributes to be decided? For example: If you buy a pig it should be presented with, Name: Pig, Age: 2, Weight: 50kg and so on?
@Bondenn: leave that to the printing code; take a look at the string format syntax to make that part easier.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.