[Tutor] Python input function not working in 2.7 version
eryksun
eryksun at gmail.com
Wed Oct 24 15:16:37 CEST 2012
On Wed, Oct 24, 2012 at 7:15 AM, Morten Engvoldsen <mortenengv at gmail.com> wrote:
>
> I am facing issue with input() of Python 2.7
> ....
>
> fat_grams = input("How many grams of fat are in one serving? ")
> total_calories = input("How many total calories are in one serving? ")
> print("A food product having {0} fat grams and {1} total
> calories",fat_grams, total_calories);
> return;
Avoid using input() in Python 2.x. It evaluates arbitrary expressions
that can be a security risk at worst, but at least a source of bugs.
Instead use raw_input(). This returns an unevaluated string. You can
convert it to a number with int():
>>> grams = int(raw_input("How many grams? "))
How many grams? 28
>>> grams
28
Also, "print" is a statement in 2.x; you don't call() it as a
function, not unless you import the print() function from __future__:
>>> print # print an empty line
>>> from __future__ import print_function
>>> print # now it's a function
<built-in function print>
Finally, you don't have to terminate any lines with a semicolon.
Python's grammar allows it, but it's not idiomatic style.
More information about the Tutor
mailing list