简体   繁体   中英

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

I'm making a scheduler that gets user input by their name , # of jobs they have access in and what kind of jobs are those. My problem is that if # of jobs == 4, then, kind of jobs should prompt user 4 times but since they're both inside their own functions, # of jobs resets which makes kind of jobs only prompt once.

What I've tried, combine them together in a function so that # of jobs won't reset and made a for loop but instead of prompting the user 4 times, it displays;

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

Expected result would be;

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()

Your code has some issues which I will outline below.

  1. You are adding every job access_num number of times, but you need to add it only once.
  2. Use a while loop instead of recursion to ensure the input is only done access_num number of times, and for invalid input.
  3. Define full_employee inside your function, and actually call the function to make it work
  4. Check for conditions greater than equal to 1 as well
  5. Check if the input is valid by comparing it to list of valid jobs, instead of checking isalphanum
  6. Your employee_name_input function should return a string
  7. Your isalpha for name won't work if I have a space in the name, unless you only want first name without any spaces

The code below should work for you. Check out the comments to understand what is happening

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)

Possible outputs will be.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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