【问题标题】:How to stop loop when creating an employee database?创建员工数据库时如何停止循环?
【发布时间】:2021-08-25 23:41:56
【问题描述】:

我被卡住了,我不确定我错过了哪一部分课程......一旦用户输入“完成”,我怎样才能让我的脚本结束循环?我还有更多功能要添加,因此可以通过 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"'))

【问题讨论】:

  • if name == 'done': break
  • 我一定是做错了什么,当我输入它并没有中断
  • 中断不会中断输入,它会在字符串中继续。
  • 我做到了!它需要在用户输入的那部分末尾!不是在整个动作结束时!
  • 确实……这就是它需要的地方(employee_list.append() 需要成为for 循环的一部分)

标签: python list search return edit


【解决方案1】:

试试这个:

#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"'))

【讨论】:

    猜你喜欢
    • 2014-07-21
    • 1970-01-01
    • 2018-05-13
    • 1970-01-01
    • 1970-01-01
    • 2015-12-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多