0

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

4
  • OK, and what is your question exactly? Commented Jul 18, 2014 at 22:25
  • 1
    What if there are two people named 'Jane'? Commented Jul 18, 2014 at 22:28
  • Use a dictionary, not a pair of lists. Commented Jul 18, 2014 at 22:51
  • dawg, we haven't gone that far yet; it's an intro class and we just started using lists. I guess what I was asking is how to change the variable to an index so I can use it in functions. I apologize for the lack of appropriate vocabulary, I'm new to this so it's hard for me to articulate what I want. I'm learning. Commented Jul 18, 2014 at 23:23

3 Answers 3

1

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
Sign up to request clarification or add additional context in comments.

Comments

0

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

1 Comment

@booboodingding if she doesn't want you using enumerate, you can try replacing the for line like I suggested in the first paragraph. That should also do the trick.
0

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

1 Comment

Thank you for the quick response. The assignment is that I make a bankapp that uses the input of a user and set it to a variable. Then I would pass the "custName" variable to a function, such as a deposit(custName), then have it alter the "bal" list according to the index of custName assigned during the for loop.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.