0

someone can help me to solve that my problem? that problem is,

if iam input int (1,2,3,4,5,6,7,8,9,0) always error?

data = input()

array = list(data)

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,
         "0":180,
         "1":270,
         "2":0,
         "3":90,
         "4":180,
         "5":270,
         "6":0,
         "7":90,
         "8":180,
         "9":270,
         "!":0,
         "@":90,
         "#":180,
         "$":270,
         "%":0,
         "^":90,
         "&":180,
         "*":270,
         "(":0,
         ")":90,
         "-":180,
         "_":270,}



for i in range(len(array)):

    print(array[i])

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

Error at :

ex : if am input a#2

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

KeyError: 2
4
  • int 2 vs string '2'? Try printing data and array first to help you debug... Since we don't know what's in there we can't really help. See minimal reproducible example Commented Dec 12, 2016 at 2:24
  • 1
    You should post the complete Traceback. Commented Dec 12, 2016 at 2:28
  • a#2(input), Output a 0 # 180 2 Traceback (most recent call last): File "D:**********\table.py", line 59, in <module> print(("{["+array[i]+"]}").format(table)) KeyError: 2 link Commented Dec 12, 2016 at 2:35
  • 1
    It looks like you are trying to construct an [element_index] the format specification language doesn't support. You can't quote element_index and the format language checks to see if [element_index] is an integer and passes that before it uses it as a string, unfortunately you need it to be a string and that doesn't work. If array[i] was say '*' it works fine. Commented Dec 12, 2016 at 2:37

4 Answers 4

2

Unfortunately you can't use integers as string keys to a dictionary for element_index in the format language. This is a limitation of the format language, it treats an integer element_index as an integer. Unfortunately this is not explicitly stated in the documentation https://docs.python.org/3.5/library/string.html#formatspec other than saying:

element_index ::= integer | index_string

>>> "{[2]}".format({'2':0})
KeyError: 2
>>> "{[*]}".format({'*':0})
'0'
>>> "{[2]}".format({2:0})
'0'
Sign up to request clarification or add additional context in comments.

Comments

1

From the docs for the field_name:

The field_name itself begins with an arg_name that is either a number or a keyword. If it’s a number, it refers to a positional argument, ...

and

Because arg_name is not quote-delimited, it is not possible to specify arbitrary dictionary keys (e.g., the strings '10' or ':-]') within a format string.

The grammar spec for field_name is shown as

field_name        ::=  arg_name ("." attribute_name | "[" element_index "]")*

I think the parenthesis/brackets are saying that arg_name can be either a dotAttribute or an index expression, [2] so the arbitrary dictionary key of the form '10' limitation applies - if that is correct then the docs could be clearer.

>>> d
{'1': 123, 'a': 4}

Using '''{['1']}''' as the format string, returns a double quoted string which just doesn't work.

>>> '''{['1']}'''.format(d)
Traceback (most recent call last):
  File "<pyshell#98>", line 1, in <module>
    '''{['1']}'''.format(d)
KeyError: "'1'"

>>> d.__getitem__("'1'")
Traceback (most recent call last):
  File "<pyshell#100>", line 1, in <module>
    d.__getitem__("'1'")
KeyError: "'1'"

Then using '''{1}''' for the format string creates an integer which is passed to __getitem__

>>> '''{[1]}'''.format(d)
Traceback (most recent call last):
  File "<pyshell#101>", line 1, in <module>
    '''{[1]}'''.format(d)
KeyError: 1
>>>

.format just cannot make a string that looks like '2' to be passed to __getitem__


If the dictionary has a double quoted key, then it works

>>> d["'1'"] = 'foo'
>>> d
{'1': 123, "'1'": 'foo', 'a': 4}
>>> "{['1']}".format(d)
'foo'
>>>

2 Comments

This refers to an different part of the specification namely {<field_name>[<element_index>]} the OP has issues with element_index not field_name. The documentation doesn't explicitly point out the limitation of element_index but does show: element_index ::= integer | index_string.
Just to show the difference field_name can't have ':' in it but element_name can have any char except ']', e.g. "{[:]}".format({':':0}) -> '0'
0

I think you get the same result with

data = input()

for char in data:
    print(char)
    print(table[char])

Comments

0

Since your question has already been answered I wanted to point out another way you could convert characters to numbers without an extremely long dict.

chars = " abcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*()-_"

for char in chars:
    print(char, (chars.index(char)+3)%4 * 90)

To be honest I think the original way might be better because it's extremely obvious what it does, but I just wanted to show that there is an easier way.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.