0

I'm setting up a list of baseball players in which their stats are being shown and I want to support queries request by input of the users. For example if the user wants to look up a player and only knows a part of the name and runs it, that the code I wrote down can still find the player in the list containing those letters.

I can't seem to find the right combination of the for-loops and if-statements.

I have tried the following code:

all_players = the list

for player in all_players:
    request_player = input("Please provide the name of the player you want to look up? ")
    if request_player in all_players:
        print(all_players)
    else:
        print("No player with that name was found.")

I expect that if a player's name is Miquel Andujar and he is on the list, that if I write down "miqu" as input. That I get all the matching names containing "miqu".

But with the code above I only get the "No player with that name was found." back.

Juan Antonio
  • 2,451
  • 3
  • 24
  • 34
Jango III
  • 3
  • 2
  • I think you're interested in [Python regex](https://stackoverflow.com/questions/13405223/regex-match-part-of-or-whole-word). Hopefully the link helps you go in the right direction. – m13op22 Apr 23 '19 at 13:05
  • too broad question. try to narrow it, because answer requires a lot of code. – spirit Apr 23 '19 at 13:09
  • No need of regex, you can easily check a partial name using `in`, check my answer below! – Devesh Kumar Singh Apr 23 '19 at 13:13
  • Code looks horribly wrong. Everything that you put into loop is executed for every item. So you ask user to input name to search as many times, as you have in all_players. If player, that user input match exactly to one of the player names, you print all the players. – varela Apr 23 '19 at 13:21

8 Answers8

1

This should work. You iterate over the name of each player, and check if request_player is contained in the name, if he is, append it to a list, else say not found. You would also want to convert all names to lowercase before comparing via in.

all_players = ['John', 'miquel', 'miquila', 'michael']

request_player = input("Please provide the name of the player you want to look up? ")
found = False
names = []
for player in all_players:
    if request_player.lower() in player.lower():
        names.append(player)
        found = True

if found:
    print(names)
else:
    print("No player with that name was found.")

Outputs will look like:

Please provide the name of the player you want to look up? miqu
['miquel', 'miquila']

Please provide the name of the player you want to look up? james
No player with that name was found.
Devesh Kumar Singh
  • 20,259
  • 5
  • 21
  • 40
1

Let's begin by moving your input(...) outside the for-loop. We only need to take input once.

all_players = ["the list", "Joe Moped", "Miquel Andujar"]

request_player = input("Please provide the name of the player you want to look up? ")

We can also replace the if condition with a very simple re.search. Your regex can get way more complex but here's a start

import re

all_players = ["the list", "Joe Moped", "Miquel Andujar"]

request_player = input("Please provide the name of the player you want to look up? ")
for player in all_players:
    if re.search(request_player.lower(), player.lower()):
        print("Found player: {}".format(player))
        break
    else:
        print("No player with that name was found.")

This is because re.search will return None if there's no match, so the if condition will evaluate to False

For a less verbose printout we could try something like:

out = "No player with that name was found."
for player in all_players:
    if re.search(request_player.lower(), player.lower()):
        out = "Found player: {}".format(player)
        print(out)

if out == "No player with that name was found.": print(out)
Charles Landau
  • 4,187
  • 1
  • 8
  • 24
  • Cool. What would it be if I input `*`? Crash with traceback as wrong regex? If you want case insensitive search, there is `re.I` flag for this. – varela Apr 23 '19 at 13:25
  • @varela yes there are a bunch of regex errors we could produce but that's outside the scope of the question - and I cautioned OP that our regex can get arbitrarily complex depending on their needs – Charles Landau Apr 23 '19 at 13:27
0
dog = ['the','dog','is','good','pool']

user_input='oo'

for i in dog:
    if user_input in i:
        print(i)

This will print pool and good.

Here is an example similar to what you are doing. You can take in user_input as you already are. I just made it 'oo' for simplicity. Instead of looking for if request_player is in all_players, try seeing if it is in player.

Michael Smith
  • 494
  • 1
  • 5
  • 13
  • This one helped perfectly, thank you. Only thing is that the user input has to be the exact name to get the information back. For example if the player's name is miquel and I write "miqu", I get nothing back. But if I write "miquel", I get the right data as output. – Jango III Apr 23 '19 at 13:42
  • @JangoIII hmm if all-players in your if statement is changed to players that is strange. Can you post your change, I can see whats up? If it helped accepted answers are greatly appreciated :). – Michael Smith Apr 23 '19 at 13:47
  • Check my answer @JangoIII for that case, and see if it makes sense! – Devesh Kumar Singh Apr 23 '19 at 14:11
0

There are a few elements to this that you may want to simplify.

First, since you're looping over all_players, it will only ever ask the user N times where N = len(all_players). I suspect you actually want the program to ask once (or repeatedly). Also, the actual process of checking if the user's requested player (or partial name) is in the list should probably be made into a function.

I would suggest writing your program as...

all_players = [
    'Barry Bonds',
    'Babe Ruth',
    # and more...
]

# lets define a function that takes a partial name and checks it
def find_player(partial_name, all_players):
    for player_name in all_players:
        # this is the actual matching logic.
        # you can use Regex to get fancy, but this works
        if partial_name in player_name:
            return player_name
    # it wasn't found, technically this line is redundant
    return None

# now loop to ask the user who they're looking for
while True:
    request_player = input("Please provide the name of the player you want to look up? ")
    player_name = find_player(request_player, all_players)
    if player_name
        print(player_name)
    else:
        print("No player with that name was found.")

Note the above example will only return the first player that matches the partial. So if you had typed "Ba" it would only return the first player in all_players that matches it. Also, the sample above assumes names are type-sensitive, so typing "ba" would not find any players.

Andrew F
  • 2,690
  • 1
  • 14
  • 25
0
  • You want to ask for the query outside the loop.
  • You want to check if it is inside the name of a particular player, rather than if it is exactly the input of the list input.
  • If nothing is found, then you print out the not found message.

    all_players = ['Miquel Andujar', 'abd']
    request_player = input("Please provide the name of the player you want to look up? ")
    request_player = request_player.lower()
    found = False
    for player in all_players:
        if request_player in player.lower():
            print(player)
            found = True
    if not found:
        print("No player with that name was found.")
    
Siong Thye Goh
  • 3,518
  • 10
  • 23
  • 31
0

Maybe you can use list comprehension:

matching_players = [matching_player for matching_player in all_players if request_player in matching_players]

Lax_Sam
  • 1,099
  • 2
  • 14
  • 32
0

You can try like that

all_players = ['ashish','neha','riya','rahul']
for player in all_players:
    request_player = input("Please provide the name of the player you want to look up? ")
    if request_player in player:
        print(all_players)
        print(player)
    else:
        print("No player with that name was found.")
Ashish Kumar Saxena
  • 4,400
  • 8
  • 27
  • 48
  • So if one player matches from 10, you will print 1 time this player, 1 time all players and 9 times No player with that name was found? – varela Apr 23 '19 at 13:22
0

I think this answers your question, and I hope the comments explain what I've done. As always, there are many ways to answer a question!

# I added some names...
all_players = ('Miquel Andujar', 'Jonathan Groves', 'Teresa May', 'Jean-Claude Juncker')
# Use raw_input instead...
request_player = raw_input('Please provide the name of the player you want to look up? ')
# Get a lower-case version of the input string (we'll use lower-case for all string comparisons, but this doesn't
# affect the input list, nor what the user enters):
request_player_lower = request_player.lower()
# Set a flag to be set if we find the player:
found = False
# Loop through players:
for player in all_players:
    # Get a lower-case version of this player:
    player_lower = player.lower()
    # Search for our player name in this lower-case player name:
    if (player_lower.find(request_player_lower) != -1):
        # Found them! Tell human:
        print('Found: ' + player)
        # Set flag
        found = True
# Did we find the player?
if not found:
    # No - Tell human.
    print('Not found.')
Jonathan
  • 285
  • 1
  • 10