Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python

Assigning variables to a dictionary

I am building a Baseball game to practice with loops and dictionaries. I want to run a loop of 6 pitches. Log those six pitches to a dictionary (pitch_sequence). Then run that loop 9 times for 9 innings. I want to log all the pitches by innings in a separate dictionary(psi1).

So the dictionary (pitch_sequence) is a temporary holder until it gets put into the (psi1) variable. The code is creating a dictionary with the keys i want (psi1[innings {0}.format(inning)]). but I can't figure out how to place the (pitch_sequence) dictionary into the right key after the the (pitch_count) loop runs. Any help would be appreciated.

Here is my code so far.

welcome()
innings = 1
print('inning # {}'.format(innings))
while (innings <= 9):
    pitch_count = 1
    while (pitch_count <= 6):
        thrown = random.choice(pitches)
        print ("Pitch number {} was a {}".format(pitch_count,thrown))

        pitch1Speed = range(87,91)
        pitch2Speed = range(87,90)
        pitch3Speed = range(84,89)
        pitch4Speed = range(75,79)
        pitch5Speed = range(78,82)
        if thrown == pitch_selection[0]:
            pitch_speed = random.choice(pitch1Speed)
            print ("The pitch was {} mph".format(pitch_speed))
        elif thrown == pitch_selection[1]:
            pitch_speed = random.choice(pitch2Speed)
            print ("The pitch was {} mph".format(pitch_speed))
        elif thrown == pitch_selection[2]:
            pitch_speed = random.choice(pitch3Speed)
            print ("The pitch was {} mph".format(pitch_speed))
        elif thrown == pitch_selection[3]:
            pitch_speed = random.choice(pitch4Speed)
            print ("The pitch was {} mph".format(pitch_speed))
        elif thrown == pitch_selection[4]:
            pitch_speed = random.choice(pitch5Speed)
            print ("The pitch was {} mph".format(pitch_speed))    
        else:
            print("The radar gun missed that pitch!")

        psi1['Inning {}'.format(innings)] = {}
        pitch_sequence['Pitch {}'.format(pitch_count)] = "{} mph {}".format(pitch_speed,thrown)
        for k in psi1.keys():
            if psi1.keys() == innings:
                psi1.values(pitch_sequence)
        pitch_count = pitch_count + 1
    innings = innings + 1

5 Answers

I finally figured it out. I needed to put the (pitch_sequence) dictionary before the (psi1)dictionary. Then just assign the (pitch_sequence) to the (psi1) dictionary. Then I sorted them so the print out in order. The new code is below.

    pitch_sequence['Pitch {}'.format(pitch_count)] = "{} mph {}".format(pitch_speed,thrown)
    orderedPS = collections.OrderedDict(sorted(pitch_sequence.items()))
    psi1['Inning {}'.format(innings)] = orderedPS
    orderedPSI1 = collections.OrderedDict(sorted(psi1.items()))
  1. Here is what it prints out.
  2. Inning 5
  3. Pitch 1 : 89 mph sinker
  4. Pitch 2 : 87 mph sinker
  5. Pitch 3 : 88 mph fourseam
  6. Pitch 4 : 87 mph cutter
  7. Pitch 5 : 80 mph changeup
  8. Pitch 6 : 81 mph changeup
  9. Inning 6
  10. Pitch 1 : 78 mph curve
  11. Pitch 2 : 89 mph sinker
  12. Pitch 3 : 78 mph changeup
  13. Pitch 4 : 89 mph sinker
  14. Pitch 5 : 87 mph sinker
  15. Pitch 6 : 86 mph cutter
Chris Howell
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Chris Howell
Python Web Development Techdegree Graduate 49,703 Points

I know you said you have figured whatever you were looking for out. I am not a HUGE baseball fan but I found general interest in it to play with some Python.

Basically I am taking your code output and writing my own around what I see, so you can just see it another way. I got a little confused on to what your overall goal was in the mix of the dictionary code so Ill put some questions at the bottom of this post.

If you are familiar with Workspaces I made a workspace so you can check it out and Fork it, Baseball

feel free to copy any of the code in there, im in it for the practice and fun of it as well

At the moment all it is basically doing is generating 9 innings, for each inning is printing out 6 pitches that are randomly created. No dictionaries are being used yet.

To see what that code is outputting.

in workspaces console

python baseball_game.py

So some questions for you if you'd like help.

  1. What are the requirements of the code? Example:
    • pitches must be between n1 and n2
    • all the types of thrown balls ( sinker, cutter, etc )
    • will it always be 6 throws per inning or do you want to add variable amounts?
  2. What is a sample of what 1 dictionary item is holding?

I have the file uploaded to my github account. ( so you can see the other variables that were left out.)

I am playing around with historical data from a specific pitcher. I am seeing if I can successfully predict a pitch sequence to a specific batter. Right now it just throws randoms form a (pitches) list. but as I continue to work on it, it will take into account the balls and strikes, runners on base, innings, previous pitches thrown, batters strengths and weaknesses. many other things as well. I love baseball so for me it is easier to learn when you are applying it to something you like.

Chris Howell
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Chris Howell
Python Web Development Techdegree Graduate 49,703 Points

Working on a project in a category you love is definitely a great way to learn fast.

I will go have a look at that code in your repo shortly, I am working another solution for another student. But shouldn't be too long. :-)

Chris Howell
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Chris Howell
Python Web Development Techdegree Graduate 49,703 Points

Ahh okay, so I see a bunch also commented out or placeholders.

So its looking like you'll be wanting to eventually be storing the state (into file or database) of not only pitchers but potentially batters as well?

How much Object Oriented Python have you delved into? I could Fork your code and work on building some Classes that may be easier to work with states of an object especially when you start looking at storing the states of players in a File Based system or even something like SQL database.

But it looks like you have been hacking away at this for some time. There is quite a bit in there to work with already. :-)

I have only been programming for a little over a month. So to answer your question I have no experience in object oriented python. I am going through the python track on team treehouse but haven't got to the object oriented section. I am just using the very basics I have learned so far.

Feel free to do whatever you want with the code. I appreciate any advice or help I can get.

Chris Howell
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Chris Howell
Python Web Development Techdegree Graduate 49,703 Points

No worries I am just gauging where you are at, it'll help me understand what parts of any code I write to explain more or place extra comments.

I will play around with some stuff. I can get in touch with you using the email listed on your GitHub?

Yah that email is best