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!
#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


Messages In This Thread
RE: NEW TO THIS! NEED TIPS AND CRITIQUE PLEASE! - by buran - Aug-06-2020, 11:11 AM

Forum Jump:

User Panel Messages

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