1

I'm attempting to program a loop with a dictionary of barcodes hooked up to a scanner with a raspberry pi and it only allows certain numbers to be placed in the dictionary. The example code not working is:

variable = dict() 
variable[0425872013] = 200 

while True: 

    print('ready to scan next item') 
    buffer = input('->') 

print(variable[buffer]) 

It continues to pop up as an invalid token with the variable number:

  File "foo.py", line 2
    variable[0425872013] = 200 
                      ^
SyntaxError: invalid token

I've tried putting them in as strings and it won't register them when the barcode inputs the number.

Microwave
  • 11
  • 1
  • 4
  • 1
    First things first, You need to improve the formatting of your post. Secondly, could you describe why it is not working? Do you have any stacktrace or output you could post? Otherwise it'll be difficult to help you. – Demitrian Feb 23 '16 at 21:14
  • 1
    *How* is it not working? Do you get an error? Is the result nt what you expect? –  Feb 23 '16 at 21:21
  • Is this Python 2 or Python 3? – jwodder Feb 24 '16 at 13:22

3 Answers3

1

Note that here you have set the key as an integer. If you are using Python 2, you should input using raw_input() which makes it a string.

If it is Python 3, while input() is correct, you still get a string and not an integer.

input([prompt])

If the prompt argument is present, it is written to standard output without a trailing newline. The function then reads a line from input, converts it to a string (stripping a trailing newline), and returns that.

You should either make the keys a string '0425872013' or convert the input buffer to an integer (checking that it is a valid integer entry) myinput = int(raw_input())

Note that the integer value 045 is illegal in Python 3 because Python 2 treated it as an octal value (0o45 in Python 3).

sabbahillel
  • 4,357
  • 1
  • 19
  • 36
  • 1
    Note that he may be using Python 3, in which case `input` is correct. – glibdud Feb 24 '16 at 13:15
  • I've tried making the keys a string and the scanner isn't inputting as a string so it's not reading correctly? – Microwave Feb 24 '16 at 18:27
  • @Microwave input (in Python 3) does input a string. Issue a type(buffer) , for example when I read it in Python 2.7, type(buffer) gives . You need to check what you are getting and make the keys the same type. Alternatively, make the keys an integer and use `buffer=int(input('> '))` However, in that case set up for an exception if a noninteger value is read in. – sabbahillel Feb 24 '16 at 18:43
  • The code currently works for set numbers but others such as this one doesn't work. So isn't it already the same type? – Microwave Feb 24 '16 at 20:54
  • When running this put in `print(type(buffer))` to check what you have read in. then get the key and check the type of the key to ensure that it is the correct type. I also did `print(045) and the value output is 37 because of the automatic assumption that it is octal. – sabbahillel Feb 24 '16 at 21:18
  • @Microwave I use Python 2.7, Python 3 has a different way of handling this and does to not allow leading 0 at all. It must be input as a string and converted with int. http://stackoverflow.com/questions/15419312/leading-zeros-in-python and https://www.quora.com/Why-an-integer-with-leading-zero-showing-Syntax-Error-in-valid-token-in-Py-3-And-how-can-avoid-it – sabbahillel Feb 24 '16 at 21:30
  • @Microwave Python 2 automatically treats all integers with a leading 0 as octal. Python 3 treats all integers with a leading 0 as illegal. When running this put in print(type(buffer)) to check what you have read in. then get the key and check the type of the key to ensure that it is the correct type. do print(buffer) and if it is a string use int() to convert to integer or make set values and checks strings. I also did `print(045) and the value output is 37 because of the automatic assumption that it is octal. – sabbahillel Feb 24 '16 at 21:34
0

You assign keys as integer but you give keys as string. So try this :

print(variable[int(buffer)])
0

You need to put the barcodes as strings into the dictionary and use raw_input() instead of input(). The invalid token error comes from the number literal because the leading zero lets Python parse this as an octal number instead of a decimal one, and the number contains digits that are not allowed in octal numbers.

BlackJack
  • 4,476
  • 1
  • 20
  • 25