3
  • Make a dictionary for nine Tampa Bay Rays that are given. Use the player names as keys and a list for each value.
  • Each value list should hold the position played by the player, the batting order, and current batting average. See above.
  • When the dictionary is complete, use a for loop to display the dictionary keys and values. This is what I got for this
  • Next, use loop(s) to print the "lineup" (the dictionary in batting order). This is the step I need some help with, not sure how I go about doing an order for a dictionary like so. A list made more sense to me but that is not the question.

      main():
          rays_players = { 'DeJesus': ['DH', 6, 299],
                     'Loney': ['1B', 4, 222],
                     'Rivera': ['C', 9, 194],
                     'Forsythe': ['2B', 5, 304],
                     'Souza Jr': ['RF', 2, 229],
                     'Longoria': ['3B', 3, 282],
                     'Cabrera': ['SS', 7, 214],
                     'Kiermaier': ['CF', 1, 240],
                     'Guyer': ['LF', 8, 274] }
    
        for key in rays_players:
            print(key, rays_players[key])
       main()
    

This is what I have been trying, but it is not working, I am very new at this:

for key in sorted(rays_players.items(), key=lambda v: (v)):
    print ("%s: %s" % (key))

Step 4 is supposed to look like this:

Batting 1: CF Kiermaier, current avg: 240

Batting 2: RF Souza Jr, current avg: 229

Batting 3: 3B Longoria, current avg: 282

Batting 4: 1B Loney, current avg: 222

Batting 5: 2B Forsythe, current avg: 304

Batting 6: DH DeJesus, current avg: 299

Batting 7: SS Cabrera, current avg: 214

Batting 8: LF Guyer, current avg: 274

Batting 9: C Rivera, current avg: 194

1
  • what do you mean by "bating order"? Commented May 20, 2015 at 19:11

1 Answer 1

7

Hope this helps:

rays_players = {'DeJesus': ['DH', 6, 299],
                'Loney': ['1B', 4, 222],
                'Rivera': ['C', 9, 194],
                'Forsythe': ['2B', 5, 304],
                'Souza Jr': ['RF', 2, 229],
                'Longoria': ['3B', 3, 282],
                'Cabrera': ['SS', 7, 214],
                'Kiermaier': ['CF', 1, 240],
                'Guyer': ['LF', 8, 274]}

for key, value in sorted(rays_players.items(), key=lambda v: v[1][1]):
    print("Batting {}: {} {}, current avg: {}".format(value[1], value[0], key, value[2]))
Sign up to request clarification or add additional context in comments.

1 Comment

Awesome! Thanks! I am happy I at least had some of the same words in the code I tried xD

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.