-2

So after some comments, I've changed things around and got another error being TypeError: init() missing 4 required positional arguments: 'body', 'engine', 'brand', and 'model'. I am thinking its in the init code itself however I am new to coding so I am not too sure where it could be if not in the 2nd line of code

class car:
def __init__(self,body,engine,brand,model):
    self.body = body
    self.engine = engine
    self.brand = brand
    self.model = model
def paint_cost(self):
    if self.body == 'truck':
        return 500
    if self.body == 'sedan':
        return 300
    if self.body == 'coupe':
        return 150
    else:
        return 100
def top_speed(self):
    if self.body in ['sedan','coupe'] and self.engine == 'bigger':
            print('Faster')
    else:
            print('Slower')
    

class main(car):

       body = input('Enter the body type(truck,coupe,sedan): ')
       engine = input('Enter the engine type(bigger or smaller): ')
       brand = input('Enter the brand type: ')
       model = input('Enter the model: ')
       builtcar = car(body,engine,brand,model)
       option = input('Enter the option 1. to find paint cost or 2. to find top speed')

       if option == '1':
           print("Paint cost:",builtcar.paint_cost())
       else:
           builtcar.top_speed()

main()

Alodian
  • 1
  • 1
  • 3
    Please post the actual error message. It will have a `^` character pointing where the problem is. – Barmar May 03 '23 at 20:11
  • 1
    `def build` shouldn't be inside the `car` class. – Barmar May 03 '23 at 20:12
  • 1
    It should be `if __name__ == "__main__"`, not `if __name__ == "__build__"` This is how `__main__` works: https://www.geeksforgeeks.org/python-main-function/# – eten May 03 '23 at 20:13
  • 2
    That `if` statement should not be indented inside the class, either. – Barmar May 03 '23 at 20:14
  • 1
    Does this answer your question? [What should I do with "Unexpected indent" in Python?](https://stackoverflow.com/questions/1016814/what-should-i-do-with-unexpected-indent-in-python) – mkrieger1 May 03 '23 at 20:22
  • Why do you think you need `try/catch`? Nothing you do in the `try` raises any exceptions. – Barmar May 03 '23 at 20:24
  • Now your question body is inconsistent with the title, and also is not formatted properly anymore. – mkrieger1 May 03 '23 at 20:45
  • You confused the `class` and `def` keywords in your latest example. – mkrieger1 May 03 '23 at 20:46

1 Answers1

0

The short answer is, you should read about how code blocks work. You're creating a class car, and that is the start of a block. Everything within that block must be indented together. It is the second line of code, as well as the rest of the file that you have shown us.

You should have:

class car:
    def __init__(self,body,engine,brand,model):
        self.body = body
        self.engine = engine
        self.brand = brand
        self.model = model
    def paint_cost(self):
        if self.body == 'truck':
            return 500
        ...
if __name__ == "__main__":
    ...
    model = input('Enter the model: ')
    builtcar = car(body,engine,brand,model)
    ...

This will be "inside" the class car. The return 500 is inisde the if self.body block, which is inside the def paint_code(self) block, which is itself inside the class car: block

When you no longer want to be inside the class car: block, return your indentation to that level. The if __name__ block is outside of the class car: block, and the input prompts are inside that.

You should take care of your indentations, remember that nearly every time you end a line with a colon, like class car: the next set of lines need to be indented, in order to be "inside" that block.

Also, stylistically, most class definitions have capitalizations, so it should probably be class Car: so that you can do mycar = Car(...)

Reivax
  • 135
  • 1
  • 1
  • 10