Dict Values Help

Hi, I’m new using the dict and I’m wondering If I can have a value that goes on a number of different lines,

def print_hangman(num_of_tries):
    HANGMAN_PHOTOS={
    "picture 1":
        "x-------x",
    "picture 2": 
        """ x-------x
            |
            |
            |
            |
            |""",
    "picture 3": 
        "x-------x", 
    "picture 4": 
        "x-------x",
    "picture 5": 
        "x-------x",
    "picture 6": 
        "x-------x",
    "picture 7": 
        "x-------x"}

    print(HANGMAN_PHOTOS)

For example, code. It won’t allow me to have for key picture 2 the value I assigned it

You can do this with a line continuation using a backslash, e.g.

"picture 1": \
    "x-------x",

This isn’t optimal in terms of coding style, but it works.

3 Likes

Well, yeah, but I wan’t to seperate the lines. To create the shape in picture 2 for example. How can I do that? won’t using a backslash just join them together?

What is the error you’re having and have you tried my suggestion to fix it?

The multi-line string is not a problem. Splitting up the key-value pair on to two lines without a continuation is a problem. edit: my bad

1 Like

I’m confused. How can I have a value that has a number of lines?
I have these 7 situations: https://s3.eu-west-1.amazonaws.com/data.cyber.org.il/virtual_courses/python/rolling_assignment/resources/hangman.txt
I understand what you are saying about the key-value pair

Ah. I thought you knew the answer to that because you’re already using the solution for it: a triple-quoted string! Using """ will allow you to put multiple lines of text into one string. One thing that’s tricky is how the indentation is handled–I always test by printing the result to make sure it’s formatted the way want.

1 Like

Oh, I see. Thank you very much for your help. What confused me is when you use the “”" essentially when you click enter for the next line you dont need to need to align this line to previous one. you just need to type what you wanted at the beggining… Or something like that :smiley: :smiley:

No it isn’t.

2 Likes

:exploding_head: I definitely would have lost money on that bet. I must have assumed based on a) never doing it and b) the OP saying “it won’t allow” it.

1 Like

The multi-line string is not a problem. Splitting up the key-value pair
on to two lines without a continuation is a problem.

Not inside brackets:

 >>> d = {
 ...   1: 2,
 ...   3: 4,
 ... }
 >>> d
 {1: 2, 3: 4}

Roy, can you show the complete synax error you’re getting. There’s
nothing glaringly obviously wrong with what you posted.

Cheers,
Cameron Simpson cs@cskk.id.au

1 Like

That’s not what I meant (or maybe your example didn’t format correctly). What @pochmann pointed out to me is that you can have a newline after : and it’s still okay:

>>> d = {1:
... 2}
>>> d
{1: 2}

Which I definitely didn’t think would work

2 Likes

In the future, Roy: please try to avoid phrases like “it won’t allow me” when describing a problem. This doesn’t properly tell us what went wrong, because we don’t know:

  • What is “it”?
  • How are you “not allowed”? I.e., what did you see when you tried?

Instead, show us the result you got (if possible, by copying and pasting terminal output, and formatting it like code), and carefully explain what you wanted instead, and how that is different. (It is often useful to show the exact desired result, but we also need to be able to understand, how the result should vary, if the program input is different.)

OP didn’t say anything about getting a syntax error. My experience has been that beginners are not good at correctly explaining or communicating the problem - therefore, we should try the code, observe the result, and then try to correlate that to the complaint. This is my attempt:

First, when I try the code, I get a result like:

>>> print_hangman(2)
{'picture 1': 'x-------x', 'picture 2': ' x-------x\n            |\n            |\n            |\n            |\n            |', 'picture 3': 'x-------x', 'picture 4': 'x-------x', 'picture 5': 'x-------x', 'picture 6': 'x-------x', 'picture 7': 'x-------x'}

The reason you see a result that’s all joined together on one line, is because your code prints the entire dictionary, not one of the strings looked up from it. When the dictionary is printed, the representation of each string in it is printed (i.e., the format needed to put it in Python source code), not the actual text.

If you select one of the values from the dictionary - since they are all strings - then printing that value will make it appear properly. For example, if we replace print(HANGMAN_PHOTOS) with print(HANGMAN_PHOTOS["picture 2"]), then we will see the multi-line output.

It does not make sense to use strings for the dictionary keys. Since you wrote def print_hangman(num_of_tries):, I assume that num_of_tries will be an integer, and you want to use that to choose a “picture”. So, if you use a dictionary, the keys should be the different integers that you would care about. However, even simpler than that is to just use a list or a tuple:

def print_hangman(num_of_tries):
    HANGMAN_PHOTOS=(
        "x-------x",
        """ x-------x
            |
            |
            |
            |
            |""",
        "x-------x", 
        "x-------x",
        "x-------x",
        "x-------x",
        "x-------x",
    )
    print(HANGMAN_PHOTOS[num_of_tries-1])

Remember that the indexes for a list or a tuple will start at zero, so I subtract 1 to get the right index according to the number of tries. You can adjust this sort of thing according to your program’s actual logic, of course.

There is one remaining problem: within the multi-line string, the line indentation is just normal indentation. There are many ways to deal with this, but the simplest is to just remove the extra spaces and deal with the fact that the code doesn’t “line up” properly.

2 Likes