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

我的第一次选择工作完全正常,但是 我的第二optiond不会从名单打印出来的“enterName”。我该如何解决?我试图找到答案,但我找不到足够具体的答案。印刷元素

感谢您的期待,并帮助我:)。

此外,我的另一个问题是,当试图添加多个“新员工”它会覆盖第一个,我该如何解决这个问题?

+0

的第二个问题是因为每次你选择的时间“1”的名单'record'被重建。如果你把它放在'while'之前,我想你可以解决它。 – gabra

+0

它是什么输出? – gabra

+1

另外,在最后一个循环中,你应该“打印(记录)”。我建议将列表的名称改为'records',这样你就可以在记录中进行记录,最后进行print(记录)。 –

回答

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]) 
+0

它似乎仍然覆盖它,因为当我输入两名员工,然后放入“2”时,它只显示我添加的最新员工。此外,谢谢你帮助我,这是非常appreicated :) – potatomeister

+0

@wndzyo我忘了删除'记录'在'while'循环。我编辑了答案。 – gabra

+0

非常感谢:)。我还注意到另一个问题,我不太清楚如何解决(对不起,我的Python知识是有限的,我正在学习!),但是当我添加一些员工,然后按“2”显示员工,它也增加了一个我再次输入列表,我该如何解决? – potatomeister