Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to Iterate through an array and find more than one elements

The Problem –
If I input more than one player’s names. Only the first name comes up and the program then stops.
What can I do to print all names and the W/R Percentage presented by User’s Input.

The code –

def print_player_data():
nba_data = pd.read_csv("csv_data.csv", sep=",")
dataList = []


player_names = input("Enter a list of player names: ")
player_names = player_names.split(",")
print(player_names)

for player in player_names:
    for index, row in nba_data.iterrows():
        if row["PLAYER_NAME"] == player:
            dataList.append(row["W/R_percentage"])
           
print(dataList)

            
    

print_player_data()

The Data –

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

PLAYER_NAME,TEAM_ABBREVIATION,Player Impact Rating,GP,Wins,Losses,W/R_percentage

Alex Len,ATL,0.1,77,28,49,36.36

Alex Poythress,ATL,0.069,21,7,14,33.33

Daniel Hamilton,ATL,0.07,19,7,12,36.84

DeAndre Bembry,ATL,0.081,82,29,53,35.37

>Solution :

It seems like when inputting the names, you add spaces after the comma.
Add this line before iterating through your list to remove leading whitespaces:

player_names = [name.lstrip() for name in player_names]
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading