繁体   English   中英

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

[英]How do I promt user x amount of times based on their input on the first question

我正在做调度,通过他们的名字得到用户的输入,他们有机会获得工作和什么样的工作那些。 我的问题是,如果# of jobs == 4,那么, kind of jobs应该提示用户 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()

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM