if select == '2':
print('Displaying all employees\n')
print('Employee names:\n')
for records in record:
print(record[0])
我的第一个选项非常好, 但是我的第二个选项不会从列表中打印出“enterName”。我该如何解决?我试图找到答案,但我找不到一个具体的答案。
感谢您寻找并帮助我:)。
另外,我遇到的另一个问题是,当尝试添加多个“新员工”时,它会覆盖第一个,我该如何解决这个问题?
答案 0 :(得分: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])