0

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 
2
  • 1
    You should add an input example and the desired output. See stackoverflow.com/help/mcve Commented Nov 16, 2016 at 9:01
  • range(len(y)) will fail because indexes of lists start at 0 not at 1. Commented Nov 16, 2016 at 9:02

5 Answers 5

2

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)) 

BTW, I assume that it was a simplified example, because using format here is really overkill, as this would produce the same output:

for c in y: print(c) print(table[c]) 
Sign up to request clarification or add additional context in comments.

4 Comments

thanks sir.. always use parenthesis before format method. btw iam still newbie
Explanation is accurate, and that works... in the sense that it produces the expected output. But using a .format() to fetch a mapped value from a dict... that it about the most horrible way to do it.
@Guillaume: I assumed it was a simplified example to demonstrate a problem... Anyway I've edited my post :-)
@Guillaume : sir , why iam include new array (int), i change "a":0,"b":90, "c":180, to "1":0, "2":90, "3":180 error at from array(0) (from ealry) wheter use int can't in array list?
1

I‘m not sure what you want to do. But maybe something like this?

for i in y: print(i, table[i]) 

Comments

1

This code fails and is far from elegant:

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

In Python you generally don't need to do C style iteration over a string, using a incrementing index. Just iterate over the string directly:

for letter in x: print("my letter is", letter) print("my integer is", table[letter]) 

And to convert each letter to the matching integer, generating a list, then printing it:

my_int_list = [table[letter] for letter in x] print(my_int_list) 

Last remark, you should name your variables with descriptive names, x and y are more than confusing.

Comments

0

If i understood you correctly you wanted to convert every element in y to int. You can use list comprehension for that:

y = [int(x) for x in y] 

Comments

0

I think that answer for your question will be this code:

x = str(input('Input string\n')) 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 element in x: if element in table: print '{}\n{}'.format(element, int(table[element])) 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.