python - Why does this input to int does not work properly? -


i'm trying solve in python , not sure why isn't working...

x = int(input("enter value of x: ")) y = int(input("enter value of y: ")) print(y) input("...") 

the problem y. input follows without quotes: "2 * x" have tried lot of things , researched lot (as before asking) stumped here. perhaps it's because i'm such basic user.

seems reading books python2, have installed python3. in python2, input equals eval(raw_input(prompt)), that's why when input 2 * x, evaluates value of expression , assign y.

in python3, input user input strings, not eval expression, may need explicitly eval, a bad practice , dangerous:

in [7]: x=2  in [8]: y=eval(input('input y:'))  input y:3*x   in [9]: y out[9]: 6 

all in all, use: raw_input in py2, input in py3, never use eval (or input in py2) in production code.


Comments

Popular posts from this blog

python - Subclassed QStyledItemDelegate ignores Stylesheet -

java - HttpClient 3.1 Connection pooling vs HttpClient 4.3.2 -

SQL: Divide the sum of values in one table with the count of rows in another -