Reputation: 13
I apologize in advance, I'm an absolute beginner. What I'm trying to do is have a for loop check user input against a list to see if it's contained in that list, and if it is, place the index of that list in a variable so it can be used to access an item in another list. Here is my code so far:
cust = ["Jim", "Jane", "John"]
bal = [300, 300, 300]
curCustIndex = ""
custName = input("What is your name?")
""" Let's assume the user chose "Jane" """
for i in cust:
if custname == cust[i]:
curCustIndex = i
Basically, what I want is for the curCustIndex
to be set to an index, such as [1]
for "Jane", so I can use it to correspond with an index in another list, such as the bal
list. In this case, bal[1]
would be the balance for "Jane." I've tried to search for an answer, but a lot of the related topics are a little too advanced for me understand. Oh, and yes, I'm intentionally using global variables. This is a beginner's python class.
Thanks in advance
Upvotes: 0
Views: 4440
Reputation: 6935
Instead of for i in cust
, use for i in range(len(cust))
. That will give you the index. Simply using for i in cust
will assign the actual item from your list to i
, not the index of the item in the list.
Better yet, use for i, customer in enumerate(cust)
. When you do that, i
is the index, and customer
is the actual item from the list.
for i, customer in enumerate(cust):
if custname == customer:
curCustIndex = i
Upvotes: 0
Reputation: 51990
Python lists have an index
method that returns the position of a given item:
>>> cust = ["Jim", "Jane", "John"]
>>> bal = [300, 300, 300]
>>> cust.index("John")
2
>>> bal[cust.index("John")]
300
As stated in the doc, it is an "error [to call index] if there is no such item":
>>> cust.index("Paul")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: 'Paul' is not in list
So in real code you have to wrap that in a try ... except ...
block:
cust = ["Jim", "Jane", "John"]
bal = [300, 300, 300]
custName = input("What is your name?")
try:
print(custName, "has", bal[cust.index(custName)])
except ValueError:
print("No such client:", custName)
And testing:
sh$ python3 t.py
What is your name?John
John has 300
sh$ python3 t.py
What is your name?Paul
No such client: Paul
Upvotes: 1
Reputation: 2762
The function zip
combines two lists into pairs of elements -- one in each pair taken from each list.
In [135]: zip([1,2,3], [4,5,6])
Out[135]: [(1, 4), (2, 5), (3, 6)]
So we can iterate over both customers
and balances
together.
In [136]: customers = ["Jim", "Jane", "Steve"]
In [137]: balances = [100, 987654, 31415]
In [138]: for person, balance in zip(customers, balances):
.....: if("J" in person):
.....: print("{0} has a balance of {1}".format(person, balance))
.....:
Jim has a balance of 100
Jane has a balance of 987654
However, you might consider using a different datatype, so you don't have to loop over all the balances. Dictionaries let you look up values by a "key", as follows:
In [140]: accounts = {"Jim": 100, "Jane": 987654, "Steve": 31415}
In [141]: accounts["Steve"]
Out[141]: 31415
Upvotes: 0