如何定义';自我';在python脚本中

如何定义';自我';在python脚本中,python,Python,我正在学习python中的类和方法,我有一个脚本,我不知道为什么它不起作用。脚本从控制台接受用户的整数,然后根据所需的学生帐户数,询问用户的名字、姓氏、年龄和用户名。然后,它应该打印学生名单和他们的默认密码。最后,它应该计算并打印输入的学生的平均年龄。这是我的剧本 #!/usr/bin/python def calcAverageAge(age_list): average=sum(age_list) / len(age_list) print("The averag

我正在学习python中的类和方法,我有一个脚本,我不知道为什么它不起作用。脚本从控制台接受用户的整数,然后根据所需的学生帐户数,询问用户的名字、姓氏、年龄和用户名。然后,它应该打印学生名单和他们的默认密码。最后,它应该计算并打印输入的学生的平均年龄。这是我的剧本

#!/usr/bin/python

def calcAverageAge(age_list):
    average=sum(age_list) / len(age_list)
    print("The average age of the students is:", average)
def validateAge():
    while age < 15 or age > 45:
         age=int(input("Age must be between 15 and 45"))

class CIT383Student: # class definition
    def __init__(self,first,last,age,username,current_password):#contsructor
        self.first_name=first # instantiate object create with first name, last name, age, username, and password
        self.last_name=last
        self.User_age=age
        self.uname=username
        self.pw=current_password
    def defPassword(self): # Create default password for each user
        import time  #timestamp for default password
        firstChar=self.first_name[0]
        lastChar=self.last_name[0]
        ts=time.time()
        self.defPassword = (firstChar + lastChar + ts)    # compute the default password for the user

    #def displayUsers(self):
    #    print(self.first_name, self.last_name, self.User_age, self.uname, self.pw)


age_list = []
student_list =[]
studentNumber = int(input("Please Enter the number of student accounts: "))
for _ in range(studentNumber): #For each student
    first = input("please enter the first name of the student: ")
    last = input("Please enter the last name of the student: ")
    age = input("please enter the age of the student")
    username = input("Please enter username of the student")
    CIT383Student.defPassword()
    student=CIT383Student(first,last,age,username,current_password)
    student_list.append(student)

for student in student_list:
    CIT383Student.displayUsers()
    print("\n")

calcAverageAge()

#/usr/bin/python
def平均值(年龄列表):
平均值=总和(年龄列表)/长度(年龄列表)
打印(“学生的平均年龄为:”,平均)
def validateAge():
年龄<15岁或年龄>45岁时:
年龄=整数(输入(“年龄必须介于15和45之间”))
类别CIT383学生:#类别定义
定义初始(自我、第一、最后、年龄、用户名、当前密码):\contsructor
self.first_name=first#使用名字、姓氏、年龄、用户名和密码实例化对象创建
self.last_name=last
self.User_age=年龄
self.uname=用户名
self.pw=当前密码
def defPassword(self):#为每个用户创建默认密码
导入时间#默认密码的时间戳
firstChar=self.first\u名称[0]
lastChar=self.last_name[0]
ts=时间。时间()
self.defPassword=(firstChar+lastChar+ts)#计算用户的默认密码
#def显示用户(自我):
#打印(self.first\u name、self.last\u name、self.User\u age、self.uname、self.pw)
年龄列表=[]
学生名单=[]
studentNumber=int(输入(“请输入学生帐号:”)
适用于范围内(学生编号):#适用于每个学生
first=输入(“请输入学生的名字:”)
last=输入(“请输入学生的姓氏:”)
年龄=输入(“请输入学生的年龄”)
用户名=输入(“请输入学生的用户名”)
CIT383Student.defPassword()
student=CIT383Student(第一名、最后一名、年龄、用户名、当前密码)
学生列表。附加(学生)
对于学生列表中的学生:
CIT383Student.displayUsers()
打印(“\n”)
平均值()
我收到的错误是:


Traceback (most recent call last):

    File "filepath", line 36, in <module>
        CIT383Student.defPassword(self)
NameError: name 'self' is not defined


回溯(最近一次呼叫最后一次):
文件“filepath”,第36行,在
CIT383Student.defPassword(自我)
NameError:未定义名称“self”
如果这是一个太长的问题,很抱歉。我只是被难住了。任何帮助都将不胜感激。

在第36行,您可以拨打

CIT383Student.defPassword()

您必须首先实例化
CIT383Student
defPassword()
是一个实例方法,因此当您从类本身调用它时,需要从实例调用它。

您必须先创建类的实例
CIT383Student
,然后才能对其调用
defPassword()
,因为
defPassword()
不是静态方法

c = CIT383Student(first, last, age, username, current_password)
c.defPassword()
  • 在执行实例方法之前,需要先实例化该类
  • 不能像类方法那样调用defPassword方法(实例方法)
  • 代码中还有一个问题。您没有定义当前密码变量。可能您需要添加此代码

要定义一个类,您必须执行
class MyClass():
@LeonardoScotti这不是真的第36行,
CIT383Student.defPassword()
将引发
TypeError:defPassword()缺少一个必需的位置参数:“self”
,而不是
namererror
@Timhenn98下次请包含完整的堆栈跟踪
    student=CIT383Student(first,last,age,username,current_password)
    student.defPassword()
current_password = input("Please enter current password")