0

I'm relatively new to Python (but not with programming) and I'm getting quite confused with how dictionaries work in Python. I have a program that saves the score of a current player. Whenever the game ends, I need to save the score in a dictionary with keywords 'username', 'runtime', and 'errors'. So my final output would be something like:

Score - Username - RunTime - Error

10-------Jane--------7s--------3--

15-------Brian-------7s--------3--

12-------Dave--------7s--------3--

09-------Aura--------7s--------3--

In the terminal, I should display the output of the dictionary which I think should be:

{ '10' : ['username': Jane, 'runtime': 7s, 'errors':3], '15' : ['username': Brian, 'runtime': 7s, 'errors':3], and so on and so forth

Is this possible? What should be the workaround here? Thank you for the help

6
  • 2
    1. Your inner structure are lists, not dictionaries. 2. Why would the score be the outer keys? can't 2 users get the same score? It feels like the username should be the outer key (if at all. Why not just have a list of dictionaries?, ie [{'username': 'Jane', 'runtime': '7s', 'score': 10}, {'username': 'Brian', 'runtime': '7s', 'score': 15}]) Commented Sep 17, 2018 at 20:06
  • Actually, I realized it's not the problem that confuses me. It's the instruction. The instruction says, "after the game is done, SAVE THE SCORE IN A DICTIONARY WITH KEYS USERNAME, TIME, AND ERRORS". I don't exactly know what that means or what the output should be. Does this makes sense? Commented Sep 17, 2018 at 20:09
  • The thing that will make the most sense would be to ask whoever wrote the instructions. Commented Sep 17, 2018 at 20:10
  • It's like the goal is somehow similar in a "leaderboard" concept. Commented Sep 17, 2018 at 20:10
  • 1
    Trying to make sense out of the instructions. As even score or username can repeat in the final output but combinations of username,runtime and errors might not repeat and can be treated as a key and value being the score Commented Sep 17, 2018 at 20:16

2 Answers 2

1

Each dictionary element consists of 2 parts: - key - value Keys should be unique and immutable (string, integers, sets etc..) The score is immutable (integer) but probably not unique. In case several players get the same amounts of scores, you will not be able to store their data under the same key.

Consider the following structures:

  1. DICTIONARY WITH USERNAME AS A KEY {Username: [Score, RunTime, Error], ...}

  2. LIST OF LISTS [[Username, Score, RunTime, Error], ...]

Sign up to request clarification or add additional context in comments.

1 Comment

dictionary with username as a key might work and makes more sense. I just currently don't know how to do it syntactically (assigning list as a value for the key) but will explore it now. Thank you very much.
0

To save the score in a dictionary with keys username :

scores = {}
scores["Name"] = [runtime, score]

then use scores["Name"][0] or score["Name"][1]

Or if you realy want to use the keys like "score" or "runtime" you can use an other dict in the score dict.

scores = {}
scores["Name"] = {"runtime": 10, "score": 100}

And use scores["Name"]["score"] or scores["Name"]["runtime"] to get the score or the runtime.

Comments

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.