eval evaluates the python expression. In python 3, numbers starting by 0 aren't allowed (except for 0000, see Why does 000 evaluate to 0 in Python 3?). In python 2, those are interpreted as octal (base 8) numbers. Not better... (python 3 base 8 now uses exclusively Oo prefix)
int performs a string to integer conversion, so it cannot evaluate a complex expression (that you don't need), but isn't subjected to this leading zero syntax.
Another nice feature is that you can check if the entered expression is an integer by using a simple and qualified try/except block:
Copywhile True:
try:
age = int(input("enter age"))
break
except ValueError:
print("Retry!")
(with eval you would have to protect against all exceptions)
Advice: use int, because it's safer, doesn't have security issues (eval can evaluate any expression, including system calls and file deletion), and suits your purpose perfectly.
Note: the above code is still unsafe with python 2: input acts like eval. You could protect your code against this with the simple code at the start of your module:
Copytry:
input = raw_input
except NameError:
pass
so python 2 input is not unreachable anymore and calls raw_input instead. Python 3 ignores that code.
eval evaluates the python expression. In python 3, numbers starting by 0 aren't allowed (except for 0000, see Why does 000 evaluate to 0 in Python 3?). In python 2, those are interpreted as octal (base 8) numbers. Not better... (python 3 base 8 now uses exclusively Oo prefix)
int performs a string to integer conversion, so it cannot evaluate a complex expression (that you don't need), but isn't subjected to this leading zero syntax.
Another nice feature is that you can check if the entered expression is an integer by using a simple and qualified try/except block:
Copywhile True:
try:
age = int(input("enter age"))
break
except ValueError:
print("Retry!")
(with eval you would have to protect against all exceptions)
Advice: use int, because it's safer, doesn't have security issues (eval can evaluate any expression, including system calls and file deletion), and suits your purpose perfectly.
Note: the above code is still unsafe with python 2: input acts like eval. You could protect your code against this with the simple code at the start of your module:
Copytry:
input = raw_input
except NameError:
pass
so python 2 input is not unreachable anymore and calls raw_input instead. Python 3 ignores that code.
eval() is used to verify an expression. On number is considered an expression, except octal numbers (numbers that start with 0). int() handles string to integer conversion. There are many reasons why you should avoid using eval(). Just keep in mind:
Python 2.x
Copyx = raw_input('Enter number here: ')Python 3.x
Copyx = input('Enter number here: ')Python 2.x
Security risk:Copyx = input('Enter number here: ')Python 3.x
Security risk:Copyx = eval(input('Enter number here: '))
Also, keep in mind that eval() has the potential to run code, which could cause a huge security risk. I suggest not using it unless you clearly know what you're doing or it could compromise your application.
IS there any difference between
entry = eval(input('Enter your number: '))
And
entry = int(input('Enter your number: '))
Also, yes this might sound silly but can the entry through either of these be a float value?(yes I'm new to the language please excuse me if i dont use proper terms)
Videos
int takes as a parameter (a string representing) an integer literal, not an arbitrary arithmetic expression.
eval takes a string and evaluates it, i.e. it "runs" it as if it were code. As 3**631+26 is a valid python expression (the sum of 3 to the 631st power and 26), eval works in your case, but opens your application to code injection. (What happens if the user enters e.g. __import__('sys').exit(0)?)
The int function expects a string with a sequence of digits ('0' to '9') to convert it into an integer. If there are other symbols like * then it is not able to parse that string.
Hey all,
I am getting an error from my linter when taking input from a user, so for the following code:
guess_row = int(input("Guess Row:"))
guess_col = int(input("Guess Col:"))My linter (in NINJA-IDE) is suggesting the following:
guess_row = int(eval(input("Guess Row:")))
guess_col = int(eval(input("Guess Col:")))My question is is this strictly necessary in python 3.X, if so, why? Is it stylistic or is there another reason?
Thanks!
My question is is this strictly necessary in python 3.X, if so, why? Is it stylistic or is there another reason?
NO NO NO NO NO
If your linter is suggesting that, erase all traces of it from your computer. That is a terrible suggestion. eval executes a string as Python code. If you eval a user's input, they can use your program to do literally anything... like corrupt the operating system.
The way you've done this originally is the correct way.
Hi! I'm working on a bot to reply with suggestions for common python problems. This might not be very helpful to fix your underlying issue, but here's what I noticed about your submission:
You appear to be using eval + input like so:
var = eval(input("..."))
This should be avoided. Instead, the desired conversion should be performed explicitly, like so:
var = int(input("..."))
var = float(input("..."))