0
class Car:
    def __init__(self, mileage, make, model):
        self.mileage = mileage
        self.make = make
        self.model = model

    def printCar(self):
        return "The Model is:" + self.model + " the make is: " + self.make + " and the mileage is: " + self.mileage


car = Car(100, 'Suzuki', 'Brezza')

print(car.printCar())

Error Seen: return "The Model is:" + self.model + " the make is: " + self.make + " and the mileage is: " + self.mileage TypeError: must be str, not int

Please help, i am a beginner in python...

Codershree
  • 77
  • 9
  • 1
    100 is not a string, it is an integer. – Daniel Roseman Aug 01 '18 at 14:40
  • what if i want to pass an integer as an argument – Codershree Aug 01 '18 at 14:41
  • This means that you cannot concatenate **integers** (the mileage, the number 100) and **strings**. You need to use the `str()` function to fix it : return "The Model is:" + self.model + " the make is: " + self.make + " and the mileage is: " + str(self.mileage) – NanoPish Aug 01 '18 at 14:42
  • Just replace `self.mileage = mileage` by `self.mileage = str(mileage)` and everything else stays the same – Sheldore Aug 01 '18 at 14:46
  • 2
    Also note that repeated concatenation like this is quite inefficient, as it has to create a temporary string for the result of *each* concatenation. For a scenario like this, you can avoid that cost *and* avoid needing to worry about types (the default behavior of format strings already stringifies inputs) using format strings, changing your code to `return "The Model is:{} the make is: {} and the mileage is: {}".format(self.model, self.make, self.mileage)` (or on Python 3.6 with f-strings, `return f"The Model is:{self.model} the make is: {self.make} and the mileage is: {self.mileage}"`). – ShadowRanger Aug 01 '18 at 14:46
  • 1
    @Bazingaa: You probably don't want to change the `mileage` attribute to be a `str` permanently; mileage is naturally numeric, and you'd want to be able to work with it numerically (drive the car 10 miles, reflect it with `self.mileage += 10`). Converting to `str` should be done only to produce formatted output. – ShadowRanger Aug 01 '18 at 14:48
  • Well, my answer was of course in the scope of OP's posted framework/question. It is understood that strings can't work in numerics. That is why wrote everything else stays the same because there is only a single return statement and no other numerics. But given the OP is beginner, probably you're right – Sheldore Aug 01 '18 at 14:51
  • 1
    Additional side-note: Normally, you wouldn't make a `printCar` method like this; you'd just define the `__str__` special method (same prototype as `printCar`, just change the name to `__str__`), which would allow seamless conversion to string (you could just do `print(car)`, not `print(car.printCar())`). – ShadowRanger Aug 01 '18 at 15:10

1 Answers1

1
def printCar(self):
    return "The Model is:" + self.model + " the make is: " + self.make + " and the mileage is: " + str(self.mileage)

Notice the str function that converts int to string.

Alexander
  • 4,420
  • 7
  • 27
  • 42
Amitay Dror
  • 155
  • 6