我正在尝试让用户输入与第一个列表匹配的ID号,然后从以下2个列表中提取索引匹配名称和支付率。但是,它没有在列表中找到员工ID。我已经尝试了一堆不同的循环,但要么完全出错,让一个像“0”这样的数字返回值,或者像现在一样工作 - 很好但与第一个列表不匹配。
这是一个课堂作业,但我找不到课堂上或网上的任何类似于我想要做的例子。我在employee_id_prompt部分尝试了一堆不同的循环,但我无法让它工作。
有人可以提供一些指导,告诉我如何将输入匹配到第一个列表,并返回第二个和第三个列表中的匹配值吗?我已经尝试了一堆“for employee in employee_id”,“如果在employee_id中输入_employee_id”等,但我一直在寻找我无法弄清楚的错误。
SIZE = 10
employee_id = [1001, 1002, 1003, 1004, 1005, 1006, 1007, 1008, 1009, 1010]
employee_name = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J"]
employee_pay = [17.75, 18.23, 19.42, 20.00, 21.50, 22.34, 23.74, 24.68, 24.75, 25.00]
def check_int(prompt):
int_result = 0
while True:
try:
int_result = int(input(prompt))
except ValueError:
print("This is outside the range or invalid. Please enter again.")
continue
else:
break
return int_result
def employee_id_prompt():
input_employee_id = 0
found = False
index = 0
while True:
input_employee_id = check_int("Please enter your Employee ID number: ")
while found == False and index <= SIZE - 1:
if employee_id[index] == input_employee_id:
found = True
else:
index = index + 1
if found == True:
print("Welcome " + employee_name[index] + ". Your current pay rate is $", + employee_pay[index],
"an hour.")
break
else:
print("I'm sorry, that Employee ID number is not recognized.")
index = index + 1
employee_id_prompt()
答案 0 :(得分:0)
似乎必须在found
循环内初始化index
和while True
while True:
found = False
index = 0
input_employee_id = check_int("Please enter your Employee ID number: ")
while index <= SIZE - 1:
if employee_id[index] == input_employee_id:
found = True
break
else:
index = index + 1
有了更多的python经验,你会更喜欢list.index()
来搜索列表,就像Coldspeed所说的那样。