2015-12-05 36 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