简体   繁体   English

如何发现我在Python中的错误?

[英]How to spot my error in Python?

Write a class named Person with data attributes for a person's name, address, and telephone number. 编写一个名为Person的类,该类具有一个人的姓名,地址和电话号码的数据属性。 Next, write a class named Customer that is a subclass of the Person class. 接下来,编写一个名为Customer的类,它是Person类的子类。

The Customer class should have a data attribute for a customer number and a Boolean data attribute indicating whether the customer wishes to be on a mailing list. Customer类应该具有一个用于客户编号的数据属性和一个布尔数据属性,该属性指示该客户是否希望在邮件列表中。 Demonstrate an instance of the Customer class in a simple program.' 在一个简单的程序中演示Customer类的实例。”

This is what I have for my code, but I keep getting the following error: 这就是我的代码,但是我不断收到以下错误:

Traceback (most recent call last):
  File "/Users/ryanraben/Desktop/person1.py", line 45, in <module>
    import customer
ModuleNotFoundError: No module named 'customer'

I have been struggling with this class this semester. 这个学期我一直在为这堂课苦苦挣扎。 I found someone asking a similar question here in Stack Overflow but their code was very different than what I had (and if I copied their code I still could not get the correct results). 我在堆栈溢出中发现有人在问类似的问题,但是他们的代码与我的代码有很大不同(如果我复制他们的代码,我仍然无法获得正确的结果)。 This was a video module and I entered my code as it appeared on the instructor's screen, but obviously I didn't do it right because his code works and mine does not. 这是一个视频模块,我输入了我在教练屏幕上显示的代码,但显然我做错了,因为他的代码有效,而我的代码无效。

class Person:
    def __init__(self, name, address, phone):
        self.__name = name
        self.__address = address
        self.__phone = phone

    def set_name (self, name):
        self.__name = name

    def set_address (self, address):
        self.__address = address

    def set_phone (self, phone):
        self.__phone = phone

    def get_name (self):
        return self.__name

    def get_address (self):
        return self.__address

    def get_phone (self):
        return self.__phone

class Customer (Person):
    def __init__(self, name, address, phone, customer_number, mailing_list):
        Person.__init__(self, name, adress, phone)
        self.__customer_number = customer_number
        self.__mailing_list = mailing_list

    def set_customer_number (self, customer_number):
          self.__customer_number = customer_number

    def set_mailing_list(self, mailing_list):
          self.__mailing_list = mailing_list

    def get_customer_number(self):
        return self.__customer_number

    def get_mailing_list (self):
        return self.__mailing_list



import customer

name = input ('Name: ') 
address = input ('Address: ')
phone = input ('Phone: ')
customer_number = input ('Customer number: ')
mail = input ('Include in mailing list? (y/n): ')

if mail.lower()=='y':
    mailing_list = True
else:
    mailing_list = False

my_customer = customer.Customer (name, address, phone, customer_number, mailing_list)    

print ('Customer Information')
print ('-----------------------------')
print ('Name: ', my_customer.get_name())
print ('Address: ', my_customer.get_address())
print ('Phone: ', my_customer.get_phone())
print ('Customer number: ', my_customer.get_customer_number())
print ('Mailing list: ', my_customer.get_mailing_list())

This is a pretty common error: 这是一个非常常见的错误:

You're attempting to import the library of customer and your IDE is simply not able to find this file. 您正在尝试import customer库,而您的IDE根本找不到该文件。

Since you're defining the Class Customer I can't see any reason why you would need to import this non-existant library. 由于您正在定义Class Customer我看不到需要导入此不存在的库的任何原因。

Consequently, I suggest you delete the line import customer . 因此,建议您删除行import customer

That is unless I've misunderstood something. 除非我误解了。

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

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