首页 > 解决方案 > 如何根据用户在第一个问题上的输入提示用户 x 次

问题描述

我正在制作一个调度程序,它可以通过用户的姓名、他们可以访问的作业数量以及这些作业类型来获取用户输入。我的问题是,如果# of jobs == 4,那么,某种作业应该提示用户 4 次,但由于它们都在自己的函数中,# of jobs重置,这使得某种作业只提示一次。

我已经尝试过,将它们组合在一个函数中,以便# of jobs不会重置并创建一个 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()

标签: pythonpython-3.x

解决方案


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

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

下面的代码应该适合你。查看评论以了解正在发生的事情

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']

推荐阅读