Bagus Prasetyo W Bagus Prasetyo W - 24 days ago 7
Python Question

how to input string, output int, use array python

Someone can help me to complete this source code
am input String, change to array list, and output that int in that table

x = input()

y = list(x)

table = {" ":270,

"a":0,
"b":90,
"c":180,
"d":270,
"e":0,
"f":90,
"g":180,
"h":270,
"i":0,
"j":90,
"k":180,
"l":270,
"m":0,
"n":90,
"o":180,
"p":270,
"q":0,
"r":90,
"s":180,
"t":270,
"u":0,
"v":90,
"w":180,
"x":270,
"y":0,
"z":90,}



for i in range(len(y)):
print(y[i])
print("{["+y[i]+"]}".format(table))


Error at

print("{["+y[i]+"]}".format(table))


Example: for input
abc
the expected output should be:

a
0
b
90
c
180

Answer

The . has higher priority that the string concatenation +.

This expression "{["+y[i]+"]}".format(table) is actually evaluated as:

"{["+y[i]+("]}".format(table))

which is not what you want.

You must use parenthesis to force the concatenation before applying format method:

print(("{["+y[i]+"]}").format(table))