Reputation: 1987
I'd like to be able to index a multi-line string such as the hangman below to chose the correct string based on the number of words incorrect. When I use the code below it does not index the correct figure and I'm not sure why. Thanks.
a=list({
'''
|
O
\|
''',
'''
|
O
\|/
|
''',
'''
|
O
\|/
|
/
''',
'''
|
O
\|/
|
/ \
'''
});
for i in range(4):
print(a[i])
Output:
|
O
\|/
|
|
O
\|/
|
O
\|/
|
/
|
O
\|/
|
/
Upvotes: 1
Views: 49
Reputation: 520
a=[
'''
|
O
\|
''',
'''
|
O
\|/
|
''',
'''
|
O
\|/
|
/
''',
'''
|
O
\|/
|
/ \\
'''
]
for i in range(4):
print(a[i])
Upvotes: 1
Reputation: 15349
If you want order to be preserved, don't use {}
notation because this creates a fundamentally unordered container (either a set
or a dict
—in your case, a set
). You then convert it to a list
, but too late—order is already lost.
Instead, just say:
a = [
""""
first string
""",
# ...
]
Square brackets denote a list
, which is an ordered container.
Upvotes: 4