-3

Hello I have a program that must scan and save barcodes to a variable. Everything is working properly except for the fact that I am getting an error whenever a bar code starts with a 0.

Here is a sample of my code:

 user_code = eval(input("Hello please enter a coupon code: "))

I must use eval(input(.... because I muse pass this integer to openpyxl(to store variables in an excel file)

But everytime a barcode starts with 0 I get this error:

Hello please enter a coupon code: 0555
Traceback (most recent call last):
 File    "C:/Users/HP/Documents/PycharmProjects/coupon_scanner/coupon_scanner_V5.py", line 149, in <module>
user_code = eval(input("Hello please enter a coupon code: "))
File "<string>", line 1
0555
   ^
SyntaxError: invalid token
A. Cola
  • 49
  • 2
  • 9
  • 1
    If you turn it into an integer you'll lose the leading zero. Is that acceptable? – TigerhawkT3 Jul 13 '15 at 18:02
  • 4
    Don't just arbitrarily `eval` string inputs from the user, [that is really dangerous](https://stackoverflow.com/questions/1832940/is-using-eval-in-python-a-bad-practice) – Cory Kramer Jul 13 '15 at 18:02
  • Python numbers starting with a zero have a special meaning: 0x12 is hexadecimal, 0o12 is octal. You could try to strip leading zeros using lstrip('0'). – mzuther Jul 13 '15 at 18:03
  • related: http://stackoverflow.com/questions/11620151/what-do-numbers-starting-with-0-mean-in-python – NightShadeQueen Jul 13 '15 at 18:03
  • Also related: http://stackoverflow.com/questions/15197673/using-pythons-eval-vs-ast-literal-eval – NightShadeQueen Jul 13 '15 at 18:04
  • I am using it as a last resort because I was having difficulty then comparing it to integers from the excel cells, I am all ears for a better solution. – A. Cola Jul 13 '15 at 18:12
  • Thank you! Yeah I did't realize I was not strictly using it as an interger!!! – A. Cola Jul 13 '15 at 19:24

1 Answers1

2

Instead of this due to security reason eval is considered to be a dangerous function and integer with 0 is known as octal integer.Like wise integer with x in know as hexa integer so type conversion hapens

user_code = eval(input("Hello please enter a coupon code: "))

try this

user_code = int(input("Hello please enter a coupon code: "))   
The6thSense
  • 8,103
  • 8
  • 31
  • 65