Python Forum
New to this! Looking for tips and critique please!
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
New to this! Looking for tips and critique please!
#1
HEY, THE CODE BELOW IS AN EXPERIMENT WITH A SIMPLE PREDICTION MODEL. IT IS MY FIRST CODING, SO I'M LOOKING FOR OPINIONS ON THE QUALITY AND ANY ADVICE ON MORE EFFICIENT METHODS AS I MOVE FORWARD. PLEASE LET ME KNOW!

#python program to predict NBA winners
while True:
    team1 = input("Enter the HOME team: Enter NBA Team Name or Enter STOP to exit")
    while team1 !='Lakers' and team1 !='lakers' and team1 !='Clippers'and team1 !='clippers'and team1 !=''and team1 !='Nuggets'and team1 !='nuggets'and team1 !='Jazz'and team1 !='jazz'and team1 !='Rockets'and team1 !='rockets'and team1 !='Thunder'and team1 !='thunder'and team1 !='Mavericks'and team1 !='mavericks'and team1 !='Grizzlies'and team1 !='grizzlies'and team1 !='Blazers'and team1 !='blazers'and team1 !='Spurs'and team1 !='spurs'and team1 !='Pelicans'and team1 !='pelicans'and team1 !='Kings'and team1 !='kings'and team1 !='Suns'and team1 !='suns'and team1 !='Timberwolves'and team1 !='timberwolves'and team1 !='Warriors'and team1 !='warriors'and team1 !='Bucks'and team1 !='bucks'and team1 !='Raptors'and team1 !='raptors'and team1 !='Celtics'and team1 !='celtics'and team1 !='Heat'and team1 !='heat'and team1 !='Pacers'and team1 !='pacers'and team1 !='76ers'and team1 !='76ERS'and team1 !='Nets'and team1 !='nets'and team1 !='Magic'and team1 !='magic'and team1 !='Wizards'and team1 !='wizards'and team1 !='Hornets'and team1 !='hornets'and team1 !='Bulls'and team1 !='bulls'and team1 !='Knicks'and team1 !='knicks'and team1 !='Hawks'and team1 !='hawks'and team1 !='Pistons'and team1 !='pistons'and team1 !='Cavaliers'and team1 !='cavaliers'and team1 !='Stop'and team1 !='stop'and team1 !='STOP':
        print("Invalid Entry")
        team1 = input("Enter HOME team: Enter NBA or Enter stop to exit")
    if (team1 =='stop'):
        print("Thank you for using our service")
        break
    if (team1 =='Stop'):
        print("Thank you for using our service")
        break
    if (team1 =='STOP'):
        print("Thank you for using our service")
        break
    team2 = input("Enter the AWAY Team: NBA Team Name")
    while team2 !='Lakers' and team2 !='lakers' and team2 !='Clippers'and team2 !='clippers'and team2 !=''and team2 !='Nuggets'and team2 !='nuggets'and team2 !='Jazz'and team2 !='jazz'and team2 !='Rockets'and team2 !='rockets'and team2 !='Thunder'and team2 !='thunder'and team2 !='Mavericks'and team2 !='mavericks'and team2 !='Grizzlies'and team2 !='grizzlies'and team2 !='Blazers'and team2 !='blazers'and team2 !='Spurs'and team2 !='spurs'and team2 !='Pelicans'and team2 !='pelicans'and team2 !='Kings'and team2 !='kings'and team2 !='Suns'and team2 !='suns'and team2 !='Timberwolves'and team2 !='timberwolves'and team2 !='Warriors'and team2 !='warriors'and team2 !='Bucks'and team2 !='bucks'and team2 !='Raptors'and team2 !='raptors'and team2 !='Celtics'and team2 !='celtics'and team2 !='Heat'and team2 !='heat'and team2 !='Pacers'and team2 !='pacers'and team2 !='76ers'and team2 !='76ERS'and team2 !='Nets'and team2 !='nets'and team2 !='Magic'and team2 !='magic'and team2 !='Wizards'and team2 !='wizards'and team2 !='Hornets'and team2 !='hornets'and team2 !='Bulls'and team2 !='bulls'and team2 !='Knicks'and team2 !='knicks'and team2 !='Hawks'and team2 !='hawks'and team2 !='Pistons'and team2 !='pistons'and team2 !='Cavaliers'and team2 !='cavaliers':
        print("Invalid Entry")
        team2 = input("Enter AWAY Team: NBA Team Name")
    if team1 == team2 :
        print("You are a clever one")
    if team1 == 'Bucks'or team1 == 'bucks':
        team1wp = (.815)
    if team1 == 'Raptors'or team1 == 'raptors':
        team1wp = (.719)
    if team1 == 'Celtics'or team1 == 'celtics':
        team1wp = (.672)
    if team1 == 'Heat'or team1 == 'heat':
        team1wp = (.631)
    if team1 == 'Pacers'or team1 == 'pacers':
        team1wp = (.600)
    if team1 == '76ers'or team1 == '76ERS':
        team1wp = (.600)
    if team1 == 'Nets'or team1 == 'nets':
        team1wp = (.469)
    if team1 == 'Magic'or team1 == 'magic':
        team1wp = (.462)
    if team1 == 'Wizards'or team1 == 'wizards':
        team1wp = (.375)
    if team1 == 'Hornets'or team1 == 'hornets':
        team1wp = (.354)
    if team1 =='Bulls'or team1 =='bulls':
        team1wp = (.338)
    if team1 == 'Knicks'or team1 == 'knicks':
        team1wp = (.318)
    if team1 == 'Pistons'or team1 == 'pistons':
        team1wp = (.303)
    if team1 == 'Hawks'or team1 == 'hawks':
        team1wp = (.299)
    if team1 =='Cavaliers'or team1 =='cavaliers':
        team1wp = (.292)
    if team1 == 'Lakers'or team1 == 'lakers':
        team1wp = (.769)
    if team1 == 'Clippers'or team1 == 'clippers':
        team1wp = (.682)
    if team1 == 'Nuggets'or team1 == 'nuggets':
        team1wp = (.652)
    if team1 == 'Jazz'or team1 == 'jazz':
        team1wp = (.636)
    if team1 == 'Thunder'or team1 == 'thunder':
        team1wp = (.631)
    if team1 == 'Rockets'or team1 == 'rockets':
        team1wp = (.631)
    if team1 == 'Mavericks'or team1 == 'mavericks':
        team1wp = (.588)
    if team1 == 'Grizzlies'or team1 == 'grizzlies':
        team1wp = (.485)
    if team1 == 'Blazers'or team1 == 'blazers':
        team1wp = (.448)
    if team1 == 'Spurs'or team1 == 'spurs':
        team1wp = (.438)
    if team1 =='Kings'or team1 =='kings':
        team1wp = (.431)
    if team1 == 'Pelicans'or team1 == 'pelicans':
        team1wp = (.424)
    if team1 == 'Suns'or team1 == 'suns':
        team1wp = (.409)
    if team1 == 'Timberwolves'or team1 == 'timberwolves':
        team1wp = (.297)
    if team1 =='Warriors'or team1 =='warriors':
        team1wp = (.231)      
    if team2 == 'Bucks'or team2 == 'bucks':
        team2wp = (.815)
    if team2 == 'Raptors'or team2 == 'raptors':
        team2wp = (.719)
    if team2 == 'Celtics'or team2 == 'celtics':
        team2wp = (.672)
    if team2 == 'Heat'or team2 == 'heat':
        team2wp = (.631)
    if team2 == 'Pacers'or team2 == 'pacers':
        team2wp = (.600)
    if team2 == '76ers'or team2 == '76ERS':
        team2wp = (.600)
    if team2 == 'Nets'or team2 == 'nets':
        team2wp = (.469)
    if team2 == 'Magic'or team2 == 'magic':
        team2wp = (.462)
    if team2 == 'Wizards'or team2 == 'wizards':
        team2wp = (.375)
    if team2 == 'Hornets'or team2 == 'hornets':
        team2wp = (.354)
    if team2 =='Bulls'or team2 =='bulls':
        team2wp = (.338 )
    if team2 == 'Knicks'or team2 == 'knicks':
        team2wp = (.318)
    if team2 == 'Pistons'or team2 == 'pistons':
        team2wp = (.303)
    if team2 == 'Hawks'or team2 == 'hawks':
        team2wp = (.299)
    if team2 =='Cavaliers'or team2 =='cavaliers':
        team2wp = (.292 )
    if team2 == 'Lakers'or team2 == 'lakers':
        team2wp = (.769)
    if team2 == 'Clippers'or team2 == 'clippers':
        team2wp = (.682)
    if team2 == 'Nuggets'or team2 == 'nuggets':
        team2wp = (.652)
    if team2 == 'Jazz'or team2 == 'jazz':
        team2wp = (.636)
    if team2 == 'Thunder'or team2 == 'thunder':
        team2wp = (.631)
    if team2 == 'Rockets'or team2 == 'rockets':
        team2wp = (.631)
    if team2 == 'Mavericks'or team2 == 'mavericks':
        team2wp = (.588)
    if team2 == 'Grizzlies'or team2 == 'grizzlies':
        team2wp = (.485)
    if team2 == 'Blazers'or team2 == 'blazers':
        team2wp = (.448)
    if team2 == 'Spurs'or team2 == 'spurs':
        team2wp = (.438)
    if team2 =='Kings'or team2 =='kings':
        team2wp = (.431)
    if team2 == 'Pelicans'or team2 == 'pelicans':
        team2wp = (.424)
    if team2 == 'Suns'or team2 == 'suns':
        team2wp = (.409)
    if team2 == 'Timberwolves'or team2 == 'timberwolves':
        team2wp = (.297)
    if team2 =='Warriors'or team2 =='warriors':
        team1wp = (.231)   
        
  
    def crtadv(x,y):
            if ((team1wp * x) > (team2wp * y)) :
                print("The winner will be the", team1)
            else:
                print("The winner will be the", team2)
    crtadv(1.1,.9)       
Reply
#2
Let's start by asking you not to use ALL CAPS - that's considered yelling.
Then some thoughts (not exhaustive list):
  • You can use a data structure like dict, to get rid of these huge if statements
  • Don't repeat yourself - define a function when you have repeating code
  • when copy/paste code it's prone to error like the one on line 141 - you left team1wp, instead of changing it to team2wp
  • use meaningful names instead of x and y
  • don't use global variables, pass arguments to your functions.
  • the checks you make on lines 7-15 will never be used - you have a while loop on line 3 and user input will always be forced to be a valid name. i.e. you cannot continue
  • you don't give option for the user to stop when you ask to enter team2

so, something like this
teams =  {'Bucks': .815,
          'Raptors':.719,
          'Celtics':.672,
          'Heat':.631,
          'Pacers':.600,
          '76Ers':.600,
          'Nets':.469,
          'Magic':.462,
          'Wizards':.375,
          'Hornets':.354,
          'Bulls':.338,
          'Knicks':.318,
          'Pistons':.303,
          'Hawks':.299,
          'Cavaliers':.292,
          'Lakers':.769,
          'Clippers':.682,
          'Nuggets':.652,
          'Jazz':.636,
          'Thunder':.631,
          'Rockets':.631,
          'Mavericks':.588,
          'Grizzlies':.485,
          'Blazers':.448,
          'Spurs':.438,
          'Kings':.431,
          'Pelicans':.424,
          'Suns':.409,
          'Timberwolves':.297}

def get_team(teams):
    while True:
        user_response = input('Enter team name or stop to quit: ').title()
        if user_response == 'Stop':
            return None, None
        elif user_response in teams:
            return user_response, teams.get(user_response, 0)
        else:
            print('Invalid input')

def crtadv(home_team, home_power, home_coefficient, away_team, away_power, away_coefficient):
    if home_power * home_coefficient > away_power * away_coefficient:
        return home_team
    else:
        return away_team
        
game = []
for idx in range(2):
    team, power = get_team(teams)
    if team:
        game.append((team, power))
    else:
        break
else:
    home_team, away_team = game
    winner = crtadv(*home_team, 1.1, *away_team, .9)
    print(f"The winner will be the {winner}")
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020