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.