从元组中打印元素

3
if select == '2':
    print('Displaying all employees\n')
    print('Employee names:\n')
    for records in record:
        print(record[0])

我的第一种选项完美地工作了,但是我的第二种选项没有打印出列表中的“enterName”。我该如何解决这个问题?我试图找到答案,但没有找到具体的解决方案。

谢谢你的帮助:)

另外,我还有一个问题,就是当尝试添加多个“新员工”时,它会覆盖第一个员工,我该如何解决这个问题?


第二个问题发生的原因是每次选择“1”时,列表record都会被重新创建。如果你把它放在while之前,我认为你可以解决这个问题。 - gabra
输出是什么? - gabra
1
此外,在最后一个循环中,您应该print(records)。我建议将列表的名称更改为records,这样您就可以使用for record in records,最后使用print(record) - Lorenzo Peña
1
@gabra 你是指在 while 循环之前放置 records = [] 还是放置 "1"?如果我在之前放置 "1",那么它就不会出现在菜单中。 - potatomeister
while循环之前加上record - gabra
1个回答

1
有两个问题。第一个是打印员工。你现在打印的是整个数组,而不是for循环的每个项。
第二个问题是每次选择“1”时都会重新创建record。你应该把它放在while循环之前。
我还修复了缩进。希望现在更好了。
例如:
select=True
record=[]
while select:
    print ("""
Personnel Database 
    1. Add a new employee
    2. Display all employees
    3. Search for an employee
    4. View full-time employees
    5. View part-time employees
    6. View number of employee records in database
    7. Delete an employee from the database
    8. Exit
    Choose an option (1-7) or 8 to Exit
    """)
    select=input("What would you like to do? ")
    if select == "1":
        print("Add a new employee")
        enterName = input ("Enter employee name:")
        enterTitle = input ("Enter employee Job title:")
        enterRate = float (input ("Enter employee hourly rate:£"))
        enterService = float(input ("Enter number of years service:"))
        fulltime = input ("Is the employee full-time? Y/N:")
        if fulltime.capitalize == "Y":
            break
        elif fulltime == "N":
            break

        record.append((enterName, enterTitle, enterRate, enterService, fulltime))
        print ("The employee you have entered is:",record)

    if select == "2":
        print ("Displaying all employees")
        print ("Employee names:")
        for r in record:
           print(r[0])

它看起来仍然是在覆盖它,因为当我输入两个员工,然后输入“2”时,它只显示我添加的最新员工。同时,非常感谢您的帮助,我非常感激 :) - potatomeister
@wndzyo 我忘记在 while 循环内部删除 record。我已经编辑了答案。 - gabra
非常感谢:)我还注意到另一个问题,不太确定如何解决(抱歉,我的Python知识有限,我正在学习!),但当我添加一些员工,然后按“2”显示员工时,它也会再次将我最后输入的员工添加到列表中,我该如何解决这个问题? - potatomeister
缩进出了问题,我想现在已经修复了。 - gabra
1
谢谢你,兄弟!我总是在一些小事上出错,但有像你这样的人帮助我,我可以学到更多 :)! - potatomeister

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接