0

For some background information, I was trying to familiarise myself with objects, classes and attributes on Python.

class Plane:
def __init__(self,ModelName,brand,capacity):
    self.ModelName = ModelName
    self.brand = brand
    self.capacity = capacity

def intro_plane(self):
    print(
        "The name of the plane is " + self.ModelName + 
        ". It was released by " + self.brand +
        ". It has a capacity of " + self.capacity
        )
P1 = Plane("B737", "Boeing", 155)
P1.intro_plane()

I ran my code on VsCode and this is the result:

TypeError: can only concatenate str (not "int") to str
cat lat
  • 37
  • 3

4 Answers4

1

capacity is a int, not a string. So when you add to print function you need to change it type.

". It has a capacity of " + str(self.capacity)
MHP
  • 352
  • 3
  • 8
1

you could use f-strings in this way

print(f"The name of the plane is {self.ModelName}. It was released by {self.brand}.It has a capacity of {self.capacity}")
Kapil
  • 165
  • 1
  • 9
  • What does the extra "f" and the extra {} do? – cat lat Aug 11 '21 at 06:38
  • according the official [documentation](https://docs.python.org/3/tutorial/inputoutput.html) it makes the following string a 'string literal' which means that To use formatted string literals, begin a string with f or F before the opening quotation mark or triple quotation mark. Inside this string, you can write a Python expression between { and } characters that can refer to variables or literal values. – Kapil Aug 11 '21 at 06:40
0

As the error states:

TypeError: can only concatenate str (not "int") to str

You cannot concatenate strings and integers. This is because + between 2 strings joins them; + between 2 integers adds them.

If you mix them, an error is raised.

You can do:

print(f"The name of the plane is {self.ModelName}. It was released by {self.brand}. It has a capacity of {self.capacity}")

That way, you don't have to worry about the data type of the variables

  • That's a much much better solution. Thanks for my making it simpler! Btw what does the "f" specifically do? – cat lat Aug 11 '21 at 06:44
  • @catlat Those are [f-strings](https://realpython.com/python-f-strings/). Consider upvoting and accepting the answer :) –  Aug 11 '21 at 06:48
  • I would if I could but it says something about having minimum 15 'reputations' before being eligible to upvote. I'll upvote back when I get there! – cat lat Aug 11 '21 at 06:58
  • @catlat to accept an answer, question author with any reputation can accept it. You can select any of our answers and click on green tick –  Aug 11 '21 at 06:59
0

Your capacity variable is already classified as an int, there is no problem there.

The problem is that you can't print an int without converting it to an str.

Simply change your intro_plane function to:

def intro_plane(self):
    print(
        "The name of the plane is " + self.ModelName + 
        ". It was released by " + self.brand +
        ". It has a capacity of " + str(self.capacity)
        )

and you should be good

Aniketh Malyala
  • 2,650
  • 1
  • 5
  • 14