Reputation: 141
I'm trying to solve this in Python and not sure why it isn't working...
x = int(input("Enter the value of X: "))
y = int(input("Enter the value of Y: "))
print(y)
input("...")
The problem is Y. I input exactly as follows without quotes: "2 * x" I have tried a lot of things and researched a lot (as I do before asking) but I am stumped here. Perhaps it's because I'm such a basic user.
Upvotes: 0
Views: 2337
Reputation: 34017
seems you are reading books with python2, but you have installed python3. In python2, input
equals to eval(raw_input(prompt))
, that's why when you input 2 * x
, it evaluates the value of the expression and assign it to y
.
In python3, input
just get user input as strings, not to eval
that as an expression, you may need explicitly eval
, which is a bad practice and 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 your production code.
Upvotes: 4
Reputation: 6144
You cannot pass expression as string literals to int
this way. You can do this instead,
x = int(input("Enter the value of X: "))
y = x * 2
print(y)
input("...")
If you instead, require another value to be used in the multiplication, you can do,
x = int(input("Enter the value of X: "))
y = int(input("Enter the value of Y: "))
z = x * y
print(z)
input("...")
Upvotes: 0
Reputation: 113915
This is because 2 * x
is not an integer. It is, when you evaluate it, though; but that's not what input
does.
So what you want is this:
x = int(input("Enter the value of X: "))
y = int(input("Enter the value of Y: ")) * x
Then, input 2
, when asked for Y
Upvotes: 0