【问题标题】:Using split(): method in Python 3 to get multiple inputs在 Python 3 中使用 split(): 方法获取多个输入
【发布时间】:2015-02-23 01:07:27
【问题描述】:

我正在做一个项目,我需要实现代码,该代码将要求用户为员工目录的以下字段输入多个数据:姓氏、名字、员工编号和工资率。

【问题讨论】:

    标签: python-3.x input split


    【解决方案1】:

    这是从用户输入获取数据并将其拆分(按空格)的示例代码:

    user_input = input("enter last name, first name, employee number, and pay rate: ")
    
    a = user_input.split()
    
    print (a[0]) # last name
    print (a[1]) # first name
    print (a[2]) # employee number
    print (a[3]) # pay rate
    

    您显然可以使用其他分隔符,将其作为第一个参数传递给 split 方法。

    【讨论】:

    • 现在在询问用户是否要添加另一个员工之后使用 if/else 函数会起作用吗?最后,上述相同的代码是否适用于搜索功能,以便用户可以通过以下任一方式搜索目录:姓氏、名字和员工编号,或者只是工资率。
    猜你喜欢
    • 1970-01-01
    • 2021-04-23
    • 2022-11-23
    • 2020-01-09
    • 2012-10-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-14
    相关资源
    最近更新 更多