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.
access_num
number of times, but you need to add it only once.access_num
number of times, and for invalid input.full_employee
inside your function, and actually call the function to make it workisalphanum
employee_name_input
function should return a stringisalpha
for name won't work if I have a space in the name, unless you only want first name without any spacesThe 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.