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

2024-09-28 17:23:47 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在制作一个调度程序,它通过用户的名称来获取用户的输入,用户可以访问的作业以及这些作业是什么类型的。我的问题是,如果#of jobs==4,那么,类jobs应该提示用户4次,但由于它们都在各自的函数中,#of jobs重置,这使得类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()

Tags: 用户nameintestbuildboxinputaccess
1条回答
网友
1楼 · 发布于 2024-09-28 17:23:47

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

  1. 每个作业添加access_num次,但只需添加一次
  2. 使用while循环而不是递归来确保输入只完成access_num次,并且对于无效的输入
  3. 在函数中定义full_employee,并实际调用函数使其工作
  4. 同时检查是否存在大于等于1的条件
  5. 通过将输入与有效作业列表进行比较来检查输入是否有效,而不是检查isalphanum
  6. 你的employee_name_input函数应该返回一个字符串
  7. 如果名称中有空格,则您的isalphafor name将不起作用,除非您只希望名字中没有空格

下面的代码应该适合您。查看评论以了解发生了什么

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

相关问题 更多 >