【问题标题】:How to fix this AttributeError inside my for loop如何在我的 for 循环中修复此 AttributeError
【发布时间】: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'

【问题讨论】:

    标签: python list loops pycharm


    【解决方案1】:

    在这部分代码中,您将字符串附加到员工列表中

    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
    

    查看 employees.append(n) 这可能是一个名称(字符串)

    所以当你打电话时

    for x in employees:
    

    您尝试执行 x.n 但附加的字符串没有属性“n”

    【讨论】:

    • 不,我的意思是for x in employees 将遍历员工列表中的每个元素,看起来列表中的第一个元素是一个字符串。字符串没有名为“n”的属性,因此在您的循环中,当您在 print 语句中调用 x.n 时,它会引发您看到的 AttributeError。
    • 我会说尝试删除 employees.append() 调用,除了说 employees.append(Employees(n, d, p, r)) 的调用
    • 哦,谢谢你,我明白现在和现在它工作正常,但现在我在选项 2 上遇到另一个错误。
    • 输入员工 0 的索引 <__main__.employees object at> 被选中输入员工的每小时:2. 它似乎不知道我选择了什么索引,因为它抛出了一个无法识别的对象(? )
    • 你能评论确切的错误信息吗?这可能是因为您正在使用 e1.compute(r) 而不是 employees[y].compute(r)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-12-30
    • 1970-01-01
    • 2015-07-13
    • 1970-01-01
    • 2021-11-24
    • 1970-01-01
    相关资源
    最近更新 更多