Luke
Luke

Reputation: 387

How can I check for a string in a list of tuples and only output once if not found?

I have a list of tuples, holding information about people (one for each tuple, with things like (name, age, etc.)). I want to check the list to see whether any names match a user input. My problem is if I use a for loop I then get multiple lines returning false, rather than just one. I also cannot then ask the user to try again, until success. My current code is:

last_name = input("Please input person's last name")

for person in personList:
    if person[0] == last_name.capitalize():
        print("success")
    else:
        print("fail")

This will print out "fail" for each player, rather than just once, and will not prompt a user to try again. I know a while loop would enable multiple attempts but I can't see how to link the while with the for, and still output a "fail" only once.

As I'm trying to learn more about tuples, please don't suggest using objects. I know it would make a lot more sense but it doesn't help me understand tuples.

Upvotes: 1

Views: 56

Answers (3)

John Gordon
John Gordon

Reputation: 33335

You need two modifications: a way to stop the loop if you find a match, and a way to print 'fail' only if you found no matches in the entire list.

You can get the first modification by adding a break in the if statement, and you can get the second one by adding an else clause to the for loop, which means "run this code if the loop ran to its full completion".

for person in personList:
    if person[0] == last_name.capitalize():
        print("success")
        break
else:
    print("fail")

Upvotes: 2

Emanuel L
Emanuel L

Reputation: 101

So first of all lets understand whats happening. For each person in the tuple you ask if his name is X.

So accordingly each person will answer you: "No", until you get to the right person, and only that person will say: "Yes", and even further, unless he is the last one it will go on until the very end.

In conclusion, you're asking every single tuple to say whether it matches the user input, or not.

But there is also an easy way of fixing this. So what can we do instead?

We will just collect every answer, and then check whether our input exists in the collection.

Lets write down in code:

total_collection = []
for person in personList:
    if person[0] == last_name.capitalize():
        total_collection.append("1")
    else:
        total_collection.append("0")
if "1" in total_collection:
    print("Success!")
else:
    print("Fail...")

In this code, the string "1" represents a match, and the string "0" represents no-match. Also, this way you can say at which index the match/es was/were located.

Upvotes: 0

Filip Młynarski
Filip Młynarski

Reputation: 3612

You could simplify checking if user input value is in personList to one line like so and then check whether input matched at least once and if it did print 'success' and break loop, else print 'fail' and ask user again.

personList = [('Abc', 'Cba'), ('Xyz', 'Zyx')]

while True:
    last_name = input("Please input person's last name: ").capitalize()

    if any(last_name == i[0] for i in personList):
        print("success")
        break
    else:
        print("fail")

Output:

Please input person's last name: random
fail
Please input person's last name: xyz
success

Upvotes: 0

Related Questions