首页 > 解决方案 > 创建员工数据库时如何停止循环?

问题描述

我被卡住了,我不确定我错过了哪一部分课程......一旦用户输入“完成”,我怎样才能让我的脚本结束循环?我还有更多功能要添加,因此可以通过 SSN 进行搜索并返回格式化结果。我感到很失落!

counter = len(employee_list)

#view all employees, enter employees, Search by SSN. Format as directed.
employee_list = []
for i in range(10):
    print('Please enter information for employee ' + str(i + 1) + ':')
    name = input('Enter employee first and last name, if finished type done:')
    ssn = input('Enter SSN: ')
    phone = input('Enter phone number: ')
    email = input('Enter email: ')
    salary = input('Enter salary: ')
employee_list.append([name,ssn,phone,email,salary])
index = int(input('Which action would you like to take next? Type "VIEW ALL", "SEARCH", OR 
"EXIT"'))

标签: pythonlistsearchreturnedit

解决方案


尝试这个:

#view all employees, enter employees, Search by SSN. Format as directed.
employee_list = []
for i in range(10):
    print('Please enter information for employee ' + str(i + 1) + ':')
    name = input('Enter employee first and last name, if finished type done:')
    if name == "done":
        break
    ssn = input('Enter SSN: ')
    if ssn == "done":
        break
    phone = input('Enter phone number: ')
    if phone == "done":
        break
    email = input('Enter email: ')
    if email == "done":
        break
    salary = input('Enter salary: ')
    if salary == "done":
        break
    employee_list.append([name,ssn,phone,email,salary])

index = int(input('Which action would you like to take next? Type "VIEW ALL", "SEARCH", OR "EXIT"'))

推荐阅读