gpt4 book ai didi

python - 尝试从不同的文件调用对象,但输出仅显示空白

转载 作者:太空宇宙 更新时间:2023-11-03 21:01:30 25 4
gpt4 key购买 nike

作为作业,我制作了一个员工类(class)文件。然后我制作了一个单独的程序,它有一个菜单,用户可以从中添加、编辑、显示员工文件的内容。但是,当我尝试显示时,唯一的输出是空白。

员工.py:

class Employee:
def __init__(self, name, emplpoyeeid, department, job_title):
self.__name = ""
self.__employeeid = ""
self.__department = ""
self.__job_title = ""

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

def set_id(self, employeeid):
self.__employeeid = employeeid

def set_department(self, department):
self.__department = department

def set_job_title(self, job_title):
self.__job_title = job_title

def get_name(self):
return self.__name

def get_employeeid(self):
return self.__employeeid

def get_department(self):
return self.__department

def get_job_title(self):
return self.__job_title

实际的程序文件:

import pickle
from Employee import Employee

try:
filename=open('Employee.dat','rb')

dictionary=pickle.load(filename)

except:
dictionary={}

while True:
print('\n1. Look up an employee in the dictionary')
print('2. Add a new employee in the dictionary')
print("3. Change an existing employee's name,department and job title in the dictionary")
print('4. Delete an employee from the dicionary')
print('5. Quit the program\n')

choice=input('\nEnter a choice: ')

if choice == '1':
while True:
employeeid=input('\nEnter the ID of the employee: ')

if employeeid in dictionary:

ob=dictionary[employeeid]

print('Employee Name: ',ob.get_name())
print('Employee ID: ',ob.get_employeeid())
print('Employee department: ',ob.get_department())
print('Employee job title: ',ob.get_job_title())
break


else:
print('\nEmployee not found \n')
break



elif choice== '2':
while True:
name=input('\nEnter the name of the employee: ')

employeeid=input('\nEnter the employee id: ')

dept=input('\nEnter the employee department: ')

title=input('\nEnter the employee job title: ')

ob=Employee(name,employeeid,dept,title)

dictionary[employeeid]=ob

print("employee has been added")
break


elif choice== '3':
while True:
employeeid=input('\nEnter the employee ID to change the details: ')

if employeeid in dictionary:

ob=dictionary[employeeid]

while True:

name=input('\nEnter the new name of the employee: ')

ob.set_name(name)

dept=input('\nEnter the new department of the employee: ')

ob.set_department(dept)

title=input('\nEnter the new job title of the employee: ')

ob.set_job_title(title)

break

else:

print('\nID not found \n')



elif choice == '4':
while True:
employeeid=input('\nEnter the ID of the employee to delete: ')

if employeeid in dictionary:
del dictionary[employeeid]
print('\nEmployee data removed \n ')
else:
print("Employee data not found")

elif choice == '5':

filename=open('Employee.dat','wb')
pickle.dump(dictionary,filename)
filename.close()

else:
print('\nPlease enter a valid choice ')
  1. Look up an employee in the dictionary
  2. Add a new employee in the dictionary
  3. Change an existing employee's name,department and job title in the dictionary
  4. Delete an employee from the dicionary
  5. Quit the program

Enter a choice: 2

Enter the name of the employee: sam

Enter the employee id: 1

Enter the employee department: 2

Enter the employee job title: 3 employee has been added

  1. Look up an employee in the dictionary
  2. Add a new employee in the dictionary
  3. Change an existing employee's name,department and job title in the dictionary
  4. Delete an employee from the dicionary
  5. Quit the program 4

Enter a choice: 1

Enter the ID of the employee: 1 Employee Name: Employee ID:
Employee department: Employee job title:

  1. Look up an employee in the dictionary
  2. Add a new employee in the dictionary
  3. Change an existing employee's name,department and job title in the dictionary
  4. Delete an employee from the dicionary
  5. Quit the program

Enter a choice:

我期望最初输入的值位于第二个输出中 但它只显示空白

最佳答案

首先:您的Employee导入不正确。如果您的文件名是employee.py,您应该编写:

从员工导入员工

因为您的程序找不到名为 Employee.py 的文件。

第二:尽量不要使用while True。它很容易让你的程序陷入无限循环。例如:如果您选择第四个选项 - 从字典中删除员工 - 您将永远不会返回主菜单,因为您无法打破无限循环。

第三:您的Employee.__init__创建空字段:

class Employee:
def __init__(self, name, emplpoyeeid, department, job_title):
self.__name = ""
self.__employeeid = ""
self.__department = ""
self.__job_title = ""

并且您在主模块中没有使用任何 setter 。当然,它会为每个字段返回空字符串。将 setter 添加到主模块或将 employee.py 更改为:

class Employee:
def __init__(self, name, emplpoyeeid, department, job_title):
self.__name = name
self.__employeeid = emplpoyeeid
self.__department = department
self.__job_title = job_title

关于python - 尝试从不同的文件调用对象,但输出仅显示空白,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55678810/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com