0

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.

rae1
  • 6,066
  • 4
  • 27
  • 48
Gavin
  • 143
  • 2
  • 2
  • 10

3 Answers3

4

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.

Community
  • 1
  • 1
zhangxaochen
  • 32,744
  • 15
  • 77
  • 108
0

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

inspectorG4dget
  • 110,290
  • 27
  • 149
  • 241
  • I see, but that won't work for what I'm doing. How can I enter expressions as the value of Y via user input? Such as "x - 4", "2 * x", etc. etc. – Gavin Feb 06 '14 at 11:43
  • @Gavin: if you really want to enter an expression, you'll have to use `eval`, but that's unwise (what if I enter the command to crash the computer?). So what you could do instead, is to keep a mapping of symbols to functions in a dictionary, which you can then extract various functions out of, after parsing the input manually – inspectorG4dget Feb 06 '14 at 17:42
  • @G4adget: Thanks, that's a good idea. I found out that I needed to use eval(), but why is using eval bad (in layman's terms)? – Gavin Feb 06 '14 at 22:14
  • @Gavin: `eval` doesn't check for what you ask it to do. You might ask it to blow up the universe, and it will simply do it. For that reason, `eval` is ill advised - you might typo something horribly wrong – inspectorG4dget Feb 06 '14 at 22:17
  • Well, I don't think that it matters with this :P, but I will avoid it in the future. Thank you. – Gavin Feb 07 '14 at 22:42
  • But wait, wouldn't adding an else: counter that? (with simple scripts) – Gavin Feb 07 '14 at 22:43
0

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("...")
rae1
  • 6,066
  • 4
  • 27
  • 48
  • I see, but that won't work for what I'm doing. How can I enter expressions as the value of Y via user input? Such as "x - 4", "2 * x", etc. etc. – Gavin Feb 06 '14 at 11:38