尝试在python中运行程序时出错

时间:2018-06-26 17:35:03

标签: python

我很早就开始研究python。而且,我尝试从pycharm站点创建一个世界,并且遇到了这个错误

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

如何解决?

遵循代码:

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()

我在此处搜索了stackoverflow,但无法弄清楚案件的答案。

enter image description here

3 个答案:

答案 0 :(得分:1)

在Python中,缩进在编写函数,循环和类时很重要。确保将主函数移到类之外,以确保获得所需的输出。

答案 1 :(得分:0)

您在if __name__ == '__main__':行中有额外的缩进。查看此更新的代码:

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()

答案 2 :(得分: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()