0

I'm starting to study python, very early on. And, I tried to make a hello world, from the site of pycharm and, I came across this error,

C:\Users\henrique.carvalho\PycharmProjects\helloworld\venv\Scripts\python.exe C:/Users/henrique.carvalho/PycharmProjects/helloworld/venv/Car.py
Traceback (most recent call last):
  File "C:/Users/henrique.carvalho/PycharmProjects/helloworld/venv/Car.py", line 1, in <module>
    class Car():
  File "C:/Users/henrique.carvalho/PycharmProjects/helloworld/venv/Car.py", line 24, in Car
    my_car = Car()
NameError: name 'Car' is not defined

How to solve?

Follow the code:

class Car:
    def __init__(self):
        self.speed = 0
        self.odometer = 0
        self.time = 0

    def say_state(self):
        print("I'm going {} kph!".format(self.speed))

    def accelerate(self):
        self.speed += 5

    def brake(self):
       self.speed -= 5

    def step(self):
        self.odometer += self.speed
        self.time += 1

    def average_speed(self):
        return self.odometer / self.time

    if __name__ == '__main__':
        my_car = Car()
        print("I'm a car!")
        while True:
            action = input("What should I do?[A]ccelerate,[B]rake, show [O]dometer, or show average [S]peed?").upper()
            if action not in "ABOS" or len(action) != 1:
                print("I don't know thow to do that")
                continue
            if action == 'A':
                my_car.accelerate()
            elif action == 'B':
                my_car.brake()
            elif action == 'O':
                print("The car has driven {} kilometers".format(my_car.odometer))
            elif action == 'S':
                print("The car's average speed was {} kph".format(my_car.average_speed()))
            my_car.step()
            my_car.say_state()

I searched here on stackoverflow but I could not figure out the answer to my case.

enter image description here

  • 1
    indentation matters when writing code in python. It looks like you nested your main function under the class – usernamenotfound Jun 26 '18 at 17:37
  • Un-indent what you intended as your main program. At the moment, it's part of the class "Car", which prevents you from creating an object with the usual syntax. – Prune Jun 26 '18 at 17:38
  • `my_car = Car()` inside the definition of `Car` is incorrect. Why is this code inside the class definition? – lurker Jun 26 '18 at 17:39

3 Answers3

1

In Python, the indentation matters while writing functions, loops, and classes. Be sure to move your main function outside of the class to make sure you get the desired output.

pragmaticprog
  • 550
  • 2
  • 15
0

You have extra indentation from if __name__ == '__main__': line. Look at this updated code:

class Car:
    def __init__(self):
        self.speed = 0
        self.odometer = 0
        self.time = 0

    def say_state(self):
        print("I'm going {} kph!".format(self.speed))

    def accelerate(self):
        self.speed += 5

    def brake(self):
       self.speed -= 5

    def step(self):
        self.odometer += self.speed
        self.time += 1

    def average_speed(self):
        return self.odometer / self.time

if __name__ == '__main__':
    my_car = Car()
    print("I'm a car!")
    while True:
        action = input("What should I do?[A]ccelerate,[B]rake, show [O]dometer, or show average [S]peed?").upper()
        if action not in "ABOS" or len(action) != 1:
            print("I don't know thow to do that")
            continue
        if action == 'A':
            my_car.accelerate()
        elif action == 'B':
            my_car.brake()
        elif action == 'O':
            print("The car has driven {} kilometers".format(my_car.odometer))
        elif action == 'S':
            print("The car's average speed was {} kph".format(my_car.average_speed()))
        my_car.step()
        my_car.say_state()
Taohidul Islam
  • 5,246
  • 3
  • 26
  • 39
0
class Car:
  def __init__(self):
      self.speed = 0
      self.odometer = 0
      self.time = 0

  def say_state(self):
      print("I'm going {} kph!".format(self.speed))

  def accelerate(self):
      self.speed += 5

  def brake(self):
     self.speed -= 5

  def step(self):
      self.odometer += self.speed
      self.time += 1

  def average_speed(self):
      return self.odometer / self.time

if __name__ == '__main__':
  my_car = Car()
  print("I'm a car!")
  while True:
      action = input("What should I do?[A]ccelerate,[B]rake, show [O]dometer, or show average [S]peed?").upper()
      if action not in "ABOS" or len(action) != 1:
          print("I don't know thow to do that")
          continue
      if action == 'A':
          my_car.accelerate()
      elif action == 'B':
          my_car.brake()
      elif action == 'O':
         print("The car has driven {} kilometers".format(my_car.odometer))
      elif action == 'S':
          print("The car's average speed was {} kph".format(my_car.average_speed()))
      my_car.step()
      my_car.say_state()
Pankaj Singh
  • 863
  • 2
  • 9
  • 18