1

I am new to coding (started learning Python a week ago) and I'm having trouble with this code that prompts the user for one of two players' names (Shai Gilgeous-Alexander and Josh Giddey) and the stat they need for the player (Points, Rebounds etc). However, every time I run my code, no matter what I put in the input it will only output the Points averaged for Shai-Gilgeous Alexander.

def main():
    player_name = input("Which player on OKC Thunder's starting 5 is stats needed for?: ")
    stats_name = input("Which stat is needed for said player? (or say \"stats\" for all stats): ")
    okc_starting_5(player_name, stats_name)




def okc_starting_5(p, s):
    if 'shai' or 'Shai Gilgeous-Alexander' or 'SGA' in p.lower().strip() and 'points' or 'pts' in s.lower().strip():
        print("Shai averaged 31.4 points per game in the 2022-23 NBA Regular Season")
    elif 'shai' or 'Shai Gilgeous-Alexander' or 'SGA' in p.lower().strip() and 'rebounds' or 'reb' in s.lower().strip:
        print("Shai averaged 4.8 rebounds per game in the 2022-23 NBA Regular Season")
    elif 'shai' or 'Shai Gilgeous-Alexander' or 'SGA' in p.lower().strip() and 'assists' or 'ast' in s.lower().strip:
        print("Shai averaged 5.5 assists per game in the 2022-23 NBA Regular Season")
    elif 'shai' or 'Shai Gilgeous-Alexander' or 'SGA' in p.lower().strip() and 'stats' in s.lower().strip:
        print("Shai averaged 31.4 points, 4.8 rebounds and 5.5 assists in the 2022-23 NBA Regular Season")
    elif 'josh' or 'josh giddey' or 'giddey' in p.lower().strip() and 'points' or 'pts' in s.lower().strip():
        print("Josh averaged 16.6 points per game in the 2022-23 NBA Regular Season")
    elif 'josh' or 'josh giddey' or 'giddey' in p.lower().strip() and 'rebounds' or 'reb' in s.lower().strip():
        print("Josh averaged 7.9 rebounds per game in the 2022-23 NBA Regular Season")
    elif 'josh' or 'josh giddey' or 'giddey' in p.lower().strip() and 'assists' or 'ast' in s.lower().strip():
        print("Josh averaged 6.2 assists per game in the 2022-23 NBA Regular Season")
    elif 'josh' or 'josh giddey' or 'giddey' in p.lower().strip() and 'stats' in s.lower().strip():
        print("Josh averaged 16.6 points, 7.9 rebounds and 6.2 assists in the 2022-23 NBA Regular Season")
    else:
        print("I cannot determine which statistics you need with this information. Please try again. ")

        
main()

It doesn't matter what input I type in the program it will always just print "Shai averaged 31.4 points per game in the 2022-23 NBA Regular Season". The else statement never even prints no matter what I type, could anyone help?

Explorer
  • 39
  • 4
  • Does this answer your question? [Why does "a == x or y or z" always evaluate to True? How can I compare "a" to all of those?](https://stackoverflow.com/questions/20002503/why-does-a-x-or-y-or-z-always-evaluate-to-true-how-can-i-compare-a-to-al) – dantiston Aug 05 '23 at 23:06

3 Answers3

1

The syntax for if x or y in z is incorrect. That breaks out into if x or (y in z). In this case x is 'shai' and the truthiness of a non-empty string evaluates to True.

You would want if x in z or y in z. Alternatively you could write if any(v in z for v in (x, y)), which you could move into a helper function.

def contains_any(data, *values):
    return any(v in data for v in values)

You also are comparing a string with upper case characters to a string that still has a lowercase 'SGA' in p.lower().strip(). Make sure to normalize to lowercase everywhere.

Now you can simplify the lookups. I would also attempt to simplify prints to reduce repetitions.

def okc_starting_5(p, s):
    name = p.lower().strip()
    stats = s.lower().strip()
    season = "2022-23 NBA Regular Season"

    if contains_any(name, 'shai', 'shai gilgeous-alexander', 'sga'):
        name, points, rebounds, assists = "Shai", 31.4, 4.8, 5.5
    else if contains_any(name, 'josh', 'josh giddey', 'giddey'):
        name, points, rebounds, assists = "Josh", 16.6, 7.9, 6.2
    else:
        print("I cannot determine which player you need. Please try again. ")
        return

    if contains_any(stats, 'points', 'pts'):
        print(f"{name} averaged {points} points per game in the {season}")
    else if contains_any(stats, 'rebounds', 'reb'):
        print(f"{name} averaged {rebounds} rebounds per game in the {season}")
    else if contains_any(stats, 'assists', 'ast'):
        print(f"{name} averaged {assists} assists per game in the {season}")
    else if contains_any(stats, 'stats'):
        print(f"{name} averaged {points} points, {rebounds} rebounds and {assists} assists in the {season}")
    else:
        print("I cannot determine which statistics you need with this information. Please try again. ")
flakes
  • 21,558
  • 8
  • 41
  • 88
0

if 'shai' or ... is always be True, that's why you have Shai averaged 31.4 points per game in the 2022-23 NBA Regular Season printed all the time. You can fix this but my suggestion is to keep all the data about players and the program logic separately:

PLAYERS = [{'name': ['shai', 'Shai Gilgeous-Alexander', 'SGA'], 'points': 31.4, 'rebounds': 4.8, 'assists': 5.5},
           {'name': ['josh', 'josh giddey', 'giddey'], 'points': 16.6, 'rebounds': 7.9, 'assists': 6.2}]


def main():
    player_name = input('Which player on OKC Thunder\'s starting 5 is stats needed for?: ')
    stats_name = input('Which stat is needed for said player? (or say "stats" for all stats): ')

    for player in PLAYERS:
        for name in player['name']:
            if name.lower() == player_name.lower():
                if stats_name == 'stats':
                    print(f"{player_name} averaged {player['points']} points, {player['rebounds']} rebounds and {player['assists']} assists in the 2022-23 NBA Regular Season")
                else:
                    print(f"{player_name} averaged {player[stats_name]} {stats_name} in the 2022-23 NBA Regular Season")
                return
    print('I cannot determine which statistics you need with this information. Please try again.')


main()

Test:

Which player on OKC Thunder's starting 5 is stats needed for?: giddey
Which stat is needed for said player? (or say "stats" for all stats): assists
giddey averaged 6.2 assists in the 2022-23 NBA Regular Season
Alderven
  • 7,569
  • 5
  • 26
  • 38
0

As flakes has already mentioned before, the syntax for if x or y in z is incorrect. Apart from that, nested if else should be used for this kind of problem. If you want to go with if x in z or y in z format with nested if else you an go as below

def main():
    player_name = input("Which player on OKC Thunder's starting 5 is stats needed for?: ")
    stats_name = input("Which stat is needed for said player? (or say \"stats\" for all stats): ")
    okc_starting_5(player_name, stats_name)




def okc_starting_5(p, s):
    if 'shai' in p.lower().strip() or 'Shai Gilgeous-Alexander' in p.lower().strip() or 'SGA' in p.lower().strip():
        if 'points' in s.lower().strip() or 'pts' in s.lower().strip():
            print("Shai averaged 31.4 points per game in the 2022-23 NBA Regular Season")
        elif 'rebounds' in s.lower().strip() or 'reb' in s.lower().strip():
            print("Shai averaged 4.8 rebounds per game in the 2022-23 NBA Regular     Season")
        elif 'assists' in s.lower().strip() or 'ast' in s.lower().strip():
            print("Shai averaged 5.5 assists per game in the 2022-23 NBA Regular     Season")
        elif 'stats' in s.lower().strip():
            print("Shai averaged 31.4 points, 4.8 rebounds and 5.5 assists in the     2022-23 NBA Regular Season")
        else:
            print("I cannot determine which statistics you need with this     information. Please try again. ")
    elif 'josh' in p.lower().strip() or 'josh giddey' in p.lower().strip() or 'giddey' in p.lower().strip():
        if 'points' in s.lower().strip() or 'pts' in s.lower().strip():
            print("Josh averaged 16.6 points per game in the 2022-23 NBA Regular Season")
        elif 'rebounds' in s.lower().strip() or 'reb' in s.lower().strip():
            print("Josh averaged 7.9 rebounds per game in the 2022-23 NBA Regular     Season")
        elif 'assists' in s.lower().strip() or 'ast' in s.lower().strip():
            print("Josh averaged 6.2 assists per game in the 2022-23 NBA Regular     Season")
        elif 'stats' in s.lower().strip():
            print("Josh averaged 16.6 points, 7.9 rebounds and 6.2 assists in the     2022-23 NBA Regular Season")
        else:
            print("I cannot determine which statistics you need with this     information. Please try again. ")

        
main()
X Coder
  • 41
  • 2
  • Rather than repeat `p.lower().strip()` and `s.lower().strip()` many times, assign these values to two variables and use those. – Chris Aug 06 '23 at 01:02