gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 07:57:48 24 4
gpt4 key购买 nike

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

我尝试过,将它们组合在一个函数中,这样 作业数量 就不会重置并创建 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']

关于python - 如何根据用户对第一个问题的输入提示用户 x 次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55877451/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com