【发布时间】:2019-12-10 15:44:14
【问题描述】:
我是一个编程初学者,这是我的第一个问题,如果我犯了任何错误,请原谅我,但这是我的问题:
我的任务是用 Python 做一个“某种”员工管理系统,作为学校的“OOP”作业。说明是:
1.它必须有一个添加员工的功能,它需要用户输入姓名、职位、部门,最后是员工的比率。
2.第二个选项需要计算每小时,基本上是乘率*每小时
3.第三个选项要求您打印您输入的所有员工(所有内容都是循环的,因此可以有多个员工而不仅仅是一个。)
4.最后退出,只是终止控制台。
在我遇到第三个选项的“AttributeError”之前,一切似乎都运行良好。我似乎无法弄清楚
错误是:Traceback(最近一次调用最后一次): 文件“C:/Users/admin/PycharmProjects/activities/OOP.py”,第 58 行,在 打印(employees.index(x),x.n,x.d,x.p,x.r) AttributeError: 'str' 对象没有属性 'n'
我已经尝试过这个循环,但正如我所说,它给出了同样的错误:
elif ans == 3:
for x in employees:
print(employees.index(x), x.n, x.d, x.p, x.r)
continue
代码相当简短,但我唯一遇到的问题是选项号 3。
employees = []
running = True
类员工:
def __init__(self, n, d, p, r):
self.n = n
self.d = d
self.p = p
self.r = r
def compute(self, h):
return h * self.r
while running:
print("Choose your option: ")
print("[1] Add new employee")
print("[2] Enter hourly of employee")
print("[3] Show employee information")
print("[4] Exit")
print("Enter option: ", end="")
ans = int(input())
if ans == 1:
print("Enter employee name: ", end="")
n = input()
employees.append(n)
print("Enter department: ", end="")
d = input()
employees.append(d)
print("Enter position: ", end="")
p = input()
employees.append(p)
print("Enter rate: ", end="")
r = int(input())
employees.append(r)
employees.append(Employees(n, d, p, r))
continue
elif ans == 2:
e1 = Employees(n, d, p, r)
print("Enter the index of the employee")
y = int(input())
print(employees[y] + " is selected")
print("Enter the hourly of employee: ")
z = int(input())
print(e1.compute(r))
elif ans == 3: #Option 3
for x in employees: #This loop seems to be the problem
print(employees.index(x), x.n, x.d, x.p, x.r)
continue
elif ans == 4:
running = False
else:
print("Invalid input, please try again: ")
continue
break
我期待的结果是这样的:
Name: Greg
Department: IT
Position: Technician
Rate: 450
Name: Isaac
Department: HR
Position: Manager
Rate: 700
但我收到了这个错误:
Traceback(最近一次调用最后一次): 文件“C:/Users/admin/PycharmProjects/activities/OOP.py”,第 59 行,在 打印(employees.index(x),x.n,x.d,x.p,x.r) AttributeError: 'str' 对象没有属性 'n'
【问题讨论】: