[英]Look up salary information by name from a list
I'm writing some python code that works out a person's total pay.我正在编写一些 Python 代码来计算一个人的总工资。
I am able to do this by getting the user to input their pay, however I would like them just to be able to enter their name and then the name is searched in position 0 of a list (Eg. 0,1 0,2 0,2 etc).我可以通过让用户输入他们的工资来做到这一点,但是我希望他们能够输入他们的姓名,然后在列表的位置 0 中搜索该姓名(例如 0,1 0,2 0 ,2 等)。
I have tried using a tuple but it is not callable and the dictionary and list would not work for me either.我试过使用元组,但它不可调用,字典和列表也不适合我。
counter = 0
valid = 0
employeelist = [["thomas","2","500"], ["jake","1","750"]]
while True:
while True:
try:
name = str(input("Name:"))
except ValueError:
print("Error")
continue
else:
break
while True:
if name == employeelist[counter,0]:
print(employeelist[counter])
break
valid = 1
elif counter = 3:
print("invalid name")
break
else:
counter = counter + 1
if valid == 1:
break
months = employeelist[counter,1]
pay = employeelist[counter,1]
totalpay = int(months) * int(pay)
Edit:编辑:
I do no longer have the code with the dictionary, however I just edited the code from [counter,1] and [0,1] to [counter][1] and is working fine thank you :D我不再有字典的代码,但是我只是将代码从 [counter,1] 和 [0,1] 编辑到 [counter][1] 并且工作正常,谢谢:D
The code below is for your inner loop下面的代码用于您的内部循环
employeelist = [["thomas","2","500"], ["jake","1","750"]]
name = ""
while True:
try:
name = input("Name:")
break
except:
print "Error"
position = -1
for i, element in enumerate(employeelist):
if element[0] == name:
position = i
break
if position == -1:
print "Invalid Name"
else:
totalpay = int(employeelist[position][1]) * int(employeelist[position][2])
Your code have multiple bugs.您的代码有多个错误。 First,
valid=1
, is set after breaking the loop -meaning valid=1
,is never set.首先,
valid=1
,在打破循环后设置 - 意思是valid=1
,永远不会设置。 You also are checking elif counter = 3
this way, you should rather use two equality signs, like this: elif counter == 3
您也在以这种方式检查
elif counter = 3
,您应该使用两个等号,如下所示: elif counter == 3
The error you are getting, that list indices must be integers or slices, not tuple, you are having because you are accessing a multidimensional array the wrong way.您遇到的错误,列表索引必须是整数或切片,而不是元组,这是因为您以错误的方式访问多维数组。 Instead of
name == employeelist[counter, 0]
, it should be name == employeelist[counter][0]
.而不是
name == employeelist[counter, 0]
,它应该是name == employeelist[counter][0]
。
Your way of iterating through the array is possible, but its rather simpler using a for loop.您遍历数组的方式是可能的,但使用 for 循环更简单。
Try this way.试试这个方法。
for employees in employeelist:
if name == employees[0]:
print(employee)
valid = 1
break
If it iterated through the hole employeelist, without the if
-block running, valid = 1
, would never been set.如果它遍历孔员工列表,没有
if
-block 运行, valid = 1
,将永远不会被设置。
Working code:工作代码:
counter = 0
valid = 0
employeelist = [["thomas","2","500"], ["jake","1","750"]]
while True:
while True:
try:
name = str(input("Name: "))
except ValueError:
print("Error")
continue
else:
break
for employees in employeelist:
if name == employees[0]:
print(employees)
valid = 1
break
if valid == 1:
break
months = employeelist[counter][1]
pay = employeelist[counter][2]
totalpay = int(months) * int(pay)
print(totalpay)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.