[Tutor] Python input function not working in 2.7 version

eryksun eryksun at gmail.com
Wed Oct 24 17:31:50 CEST 2012


On Wed, Oct 24, 2012 at 10:18 AM, Morten Engvoldsen
<mortenengv at gmail.com> wrote:
>
> grams = eval(raw_input("How many grams? "))
>
> Is it good practice to write code in this way.

That's equivalent to using input().

http://docs.python.org/library/functions.html#input

It's not generally a good practice. Sometimes it might be desirable to
use eval/exec, but there needs to be a good reason. Getting a number
from a user is not a good reason. Use int(raw_input()). This might
raise a ValueError, so you need to learn how to handle exceptions, as
covered in the tutorial:

http://docs.python.org/tutorial/errors.html#handling-exceptions


More information about the Tutor mailing list