如何根据第一个问题的输入提示用户x的时间

时间:2019-04-27 05:09:34

标签: python-3.x

我正在创建一个调度程序,以按其有权访问的名称工作数量和什么工作类型来获取用户输入是那些。我的问题是,如果#个工作 == 4,则种工作应该提示用户4次,但由于它们都在自己的功能之内,所以作业数量会重置,这使得种类的作业仅提示一次。

我尝试过,将它们组合成一个函数,以使#个作业不会重置并生成for循环,而是显示4次而不提示用户;

How many jobs do you have access in? (1-4): 2
---------------------------------------------------------------------------
Please, TYPE: SMT, TEST, REWORK, BOX BUILD, SHIPPING & WASH: wash
---------------------------------------------------------------------------
['Marc', 'WASH']
---------------------------------------------------------------------------
['Marc', 'WASH', 'WASH']
---------------------------------------------------------------------------
None
---------------------------------------------------------------------------

预期结果将是

How many jobs do you have access in? (1-4): 2
---------------------------------------------------------------------------
Please, TYPE: SMT, TEST, REWORK, BOX BUILD, SHIPPING & WASH: SMT
---------------------------------------------------------------------------
Please, TYPE: SMT, TEST, REWORK, BOX BUILD, SHIPPING & WASH: TEST
---------------------------------------------------------------------------

["SMT","TEST"]
---------------------------------------------------------------------------
full_employee = []

def employee_name_input():

    while True:
        employee_name = str(input("Enter your first name: ")).strip().capitalize()

        if not employee_name.isalpha():
            print("Invalid input. Please try again!")
        else:
            full_employee.insert(0,employee_name)
            return access_jobs_input()



def access_jobs_input():

    access_num = int(input("How many jobs do you have access in? (1-4): "))
    if access_num <= 4:
        access_jobs = str(input("Please, TYPE: SMT, TEST, REWORK, BOX BUILD, SHIPPING & WASH: ")).strip().upper()

        for num in range(access_num):      
            full_employee.append(access_jobs)
            print(full_employee)

        if not access_jobs.isalpha():
            print("Your input is invalid. Please try again")
            return access_jobs_input()    

    else:
        print ("You are entering more than 4 access and it is not authorized. Please try again!")
        return access_jobs_input()

1 个答案:

答案 0 :(得分:1)

您的代码中有一些问题,我将在下面概述。

  1. 您要access_num添加每个作业的次数是多次,但是您只需添加一次即可。
  2. 使用while循环而不是递归来确保输入仅进行access_num次,并且输入无效。
  3. 在函数内部定义full_employee,并实际调用该函数以使其起作用
  4. 还要检查条件是否大于1
  5. 通过将输入与有效作业列表进行比较来检查输入是否有效,而不是检查isalphanum
  6. 您的employee_name_input函数应返回一个字符串
  7. 如果我的名字中有空格,则您的isalpha名称将不起作用,除非您只希望名字不带任何空格

以下代码应为您工作。查看评论以了解发生了什么

def employee_name_input():

    employee_name = ''

    #Try till you get a valid name
    while True:
        employee_name = str(input("Enter your first name: ")).strip().capitalize()

        #Ask to retry if invalid name, else break the loop
        if not employee_name.isalpha():
            print("Invalid input. Please try again!")
        else:
            break

    #Return name of employee
    return employee_name

def access_jobs_input():

    #List to hold input of jobs
    full_employee = []

    #List of valid jobs
    valid_jobs = ['SMT', 'TEST', 'REWORK', 'BOX BUILD', 'SHIPPING', 'WASH']

    #Try until valid access number is provided
    while True:

        # Get number of jobs
        access_num = int(input("How many jobs do you have access in? (1-4): "))

        #If access num is between 1 and 4
        if 1 <= access_num <= 4:
            idx = 0

            #While access_num jobs are provided
            while idx < access_num:

                access_jobs = str(input("Please, TYPE: SMT, TEST, REWORK, BOX BUILD, SHIPPING & WASH: ")).strip().upper()

                #If entered job is invalid, don't increment the index and ask to try again
                if access_jobs not in valid_jobs:
                    print("Your input is invalid. Please try again")

                #If entered job is valid, append it to input of jobs and increment index
                else:
                    full_employee.append(access_jobs)
                    idx+=1

            return full_employee
        #Else return empty list
        else:
            print("You are entering invalid number of access and it is not authorized. Please try again!")


employee_name = employee_name_input()
access_jobs = access_jobs_input()
result = [employee_name]+access_jobs
print(result)

可能的输出。

How many jobs do you have access in? (1-4): -1
You are entering invalid number of access and it is not authorized. Please try again!

How many jobs do you have access in? (1-4): 5
You are entering invalid number of access and it is not authorized. Please try again!

Enter your first name: decenttaro
How many jobs do you have access in? (1-4): 2
Please, TYPE: SMT, TEST, REWORK, BOX BUILD, SHIPPING & WASH: SMT
Please, TYPE: SMT, TEST, REWORK, BOX BUILD, SHIPPING & WASH: WASH
['Decenttaro', 'SMT', 'WASH']

How many jobs do you have access in? (1-4): 3
Please, TYPE: SMT, TEST, REWORK, BOX BUILD, SHIPPING & WASH: XYZ
Your input is invalid. Please try again
Please, TYPE: SMT, TEST, REWORK, BOX BUILD, SHIPPING & WASH: ABC
Your input is invalid. Please try again
Please, TYPE: SMT, TEST, REWORK, BOX BUILD, SHIPPING & WASH: BOX BUILD
Please, TYPE: SMT, TEST, REWORK, BOX BUILD, SHIPPING & WASH: XYZ
Your input is invalid. Please try again
Please, TYPE: SMT, TEST, REWORK, BOX BUILD, SHIPPING & WASH: WASH
Please, TYPE: SMT, TEST, REWORK, BOX BUILD, SHIPPING & WASH: ABC
Your input is invalid. Please try again
Please, TYPE: SMT, TEST, REWORK, BOX BUILD, SHIPPING & WASH: REWORK
['BOX BUILD', 'WASH', 'REWORK']

Enter your first name: Decenttaro
How many jobs do you have access in? (1-4): 5
You are entering invalid number of access and it is not authorized. Please try again!
How many jobs do you have access in? (1-4): -1
You are entering invalid number of access and it is not authorized. Please try again!
How many jobs do you have access in? (1-4): 2
Please, TYPE: SMT, TEST, REWORK, BOX BUILD, SHIPPING & WASH: SMT
Please, TYPE: SMT, TEST, REWORK, BOX BUILD, SHIPPING & WASH: WASH
['Decenttaro', 'SMT', 'WASH']

Enter your first name: Decenttaro
How many jobs do you have access in? (1-4): 5
You are entering invalid number of access and it is not authorized. Please try again!
How many jobs do you have access in? (1-4): -1
You are entering invalid number of access and it is not authorized. Please try again!
How many jobs do you have access in? (1-4): 2
Please, TYPE: SMT, TEST, REWORK, BOX BUILD, SHIPPING & WASH: SMT
Please, TYPE: SMT, TEST, REWORK, BOX BUILD, SHIPPING & WASH: WASH
['Decenttaro', 'SMT', 'WASH']