gpt4 book ai didi

python - 打印元组中的元素

转载 作者:行者123 更新时间:2023-11-28 18:34:04 24 4
gpt4 key购买 nike

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

我的第一个选项非常好,但是我的第二个选项没有从列表中打印出“enterName”。我该如何解决?我试图找到答案,但找不到足够具体的答案。

感谢您查看并帮助我 :)。

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

最佳答案

有两个问题。首先是打印员工。你在做什么是打印整个数组而不是 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])

关于python - 打印元组中的元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34110462/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com