0

I'm writing a program to keep track of baseball team ratings. I would like the user to be able to input the name abbreviations of the teams that have played, the runs scored for each, and have the program update their ratings accordingly.

I list each team in a list called teams and create a new list of chronological ratings for each team with an initial rating of 1500:

teams = ["ARI","ATL","BAL","BOS","CHC","CWS","CIN","CLE","COL","DET",
"HOU","KC","LAA","LAD","MIA","MIL","MIN","NYM","NYY","OAK",
"PHI","PIT","SD","SEA","SF","STL","TB","TEX","TOR","WSH"]

for team in teams:
    team = [1500]

So every team abbreviation now has an associated list with an initial value of [1500], which will be appended with each new rating. Outside that declaration, I attempt to have the user reference which team list they would like to append a new rating to (after the program calculates the difference):

def gameIn(at="None", ht="None"):
    while at.upper() not in teams:
        at = input("Enter away team abbreviation: ")
        at = at.upper()
        if at not in teams:
            print("Team selection invalid, try again")
    while True:
        try:
            atr = int(input("Runs: "))
        except ValueError:
            print('Error: improper score.')
            continue
        break
    while ht.upper() not in teams:
        ht = input("Enter home team abbreviation: ")
        ht = ht.upper()
        if ht not in teams:
            print("Team selection invalid, try again")
        elif at == ht:
            print("Home team and away team cannot be the same, try again")
    while True:
        try:
            htr = int(input("Runs: "))
        except ValueError:
            print('Error: improper score.')
            continue
        break
    htOldRating = ht[-1]
    atOldRating = at[-1]
    print(htOldRating)
    print(atOldRating)  

The last two statements will simply print the last letter in each team name, meaning the global list of teams and team rating lists are not accessed by the function and new local variables of string type are being declared. Is it possible to have the user input the list variable name they want to access and have the program recognize it as such?

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • 1
    `for team in teams: team = [1500]` does *not* create associations. It just rebinds `team` over and over again, until you finally end up with `team = [1500=` in your namespace. Perhaps you wanted to use a *dictionary* there? – Martijn Pieters Feb 26 '15 at 08:23
  • I originally used a dictionary until I realized I would need each team to have a continuously updated list instead of a single value. That isn't the problem, however, I went through and manually assigned each team their own list; i.e. 30 of `ARI = [1500]` type statements to try to fix my problem. That didn't help. – Andrew Szoke Feb 26 '15 at 08:26
  • @AndrewSzoke: no, and Antti's linked post explains how to do it instead. I used that technique in my answer below. A dictionary can map to any type of Python object, not just numbers, you can use lists there too. – Martijn Pieters Feb 26 '15 at 08:28

1 Answers1

0

The loop

for team in teams:
    team = [1500]

simply sets the variable team to [1500] each time. It creates no associations. team was first bound to one of the strings in teams, then to a list object, repeatedly.

You need to create a dictionary here, so you can map team name to rating:

ratings = {}
for team in teams:
    ratings[team] = [1500]

or simply:

ratings = {team: [1500] for team in teams}

after which you can access those rating lists in your function, by home and away team name:

htOldRating = ratings[ht][-1]
atOldRating = ratings[at][-1]
print(htOldRating)
print(atOldRating)  
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • Got it to work, thanks a bunch. If I had tried to keep it as 30 separately maintained lists, how would I have initialized them? I was fairly confident in the syntax from another SE post on the topic. – Andrew Szoke Feb 26 '15 at 08:47
  • You wouldn't, actually. You can try and muck around with the `globals()` dictionary, but that's so much harder to maintain and avoid problems with from just using a dedicated dictionary for this mapping. – Martijn Pieters Feb 26 '15 at 08:56