简体   繁体   English

NameError:使用类时未定义名称“Robot”

[英]NameError: name 'Robot' is not defined when using class

I am reading a book on Python and I have stuck with classes.我正在阅读一本关于 Python 的书,但我一直坚持上课。

An author suggests to create a class in a separate file called robot_sample_class.py with the following code:作者建议使用以下代码在名为robots_sample_class.py的单独文件中创建一个类:

class Robot():
    """
    A simple robot class
    This multi-line comment is a good place
    to provide the description of what the class
    is.
    """

    # define the initiating function.
    # speed = value between 0 and 255
    # duration = value in milliseconds
    def __init__(self, name, desc, color, owner, speed = 125, duration = 100):
            #initializes our robot
        self.name = name
        self.desc = desc
        self.color = color
        self.owner = owner
        self.speed = speed
        self.duration = duration

    def drive_forward(self):
        #simulates driving forward
        print(self.name.title() + " is driving" + " forward " + str(self.duration) + " milliseconds")

    def drive_backward(self):
        #simulates drawing backward
        print(self.name.title() + " is driving" + " backward " + str(self.duration) + " milliseconds")

    def turn_left(self):
        #simulates turning left
        print(self.name.title() + " is turning" + " right " + str(self.duration) + " milliseconds")

    def turn_right(self):
        #simulates turning right
        print(self.name.title() + " is turning" + " left " + str(self.duration) + " milliseconds")

    def set_speed(self, speed):
        #sets the speed of the motors
        self.speed = speed
        print("the motor speed is now " + str(self.speed))

    def set_duration(self, duration):
        #sets duration of travel
        self.duration = duration
        print("the duration is now " + str(self.duration))

And then, create a new file, robot_sample.py , with this code:然后,使用以下代码创建一个新文件robots_sample.py

import robot_sample_class
my_robot = Robot("Nomad", "Autonomous rover", "black", "Jeff Cicolani")

print("My robot is a " + my_robot.desc + " called " + my_robot.name)

my_robot.drive_forward()
my_robot.drive_backward()
my_robot.turn_left()
my_robot.turn_right()
my_robot.set_speed(255)
my_robot.set_duration(1000)

Although it runs in the book perfectly, I always get an error:虽然它在书中完美运行,但我总是得到一个错误:

Traceback (most recent call last):
  File "C:\Users\Vlad\Desktop\robot_sample.py", line 2, in <module>
    my_robot = Robot("Nomad", "Autonomous rover", "black", "Jeff Cicolani")
NameError: name 'Robot' is not defined

I have checked the code thoroughly and don't know what else should I do.我已经彻底检查了代码,不知道我还应该做什么。 Maybe the problem not even with the code.也许问题甚至不是代码。

You need to specifically import the Robot class from robot_sample_class.py .您需要专门从robot_sample_class.py导入 Robot 类。 Change your import statement to the following:将导入语句更改为以下内容:

from robot_sample_class import Robot

Edit: Alternatively, you can instantiate the Robot object by qualifying it with the namespace as follows:编辑:或者,您可以通过使用命名空间限定它来实例化 Robot 对象,如下所示:

my_robot = robot_sample_class.Robot("Nomad", "Autonomous rover", "black", "Jeff Cicolani")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM