Search code examples
pythonpython-2.7raw-input

Python: What does this mean in raw_input function?


So, I recently started learning python and I am in the raw_input() section.

So while I was trying out different things, I made an error (at least that's what I think for now). Can someone please explain what is the difference between the two statements?

  1. var1 = int(raw_input())

  2. var1 = int((raw_input())

I know that the first one waits for an input from the user and assigns it to the variable var1 but, in the second case, this is the output I am getting.

>>> x = int((raw_input()) On pressing enter, it just shows the ellipses and waits for a user input.

... 12 Twelve was my input and then I get the following error.

File "<stdin>", line 2 12 ^ SyntaxError: invalid syntax

I know it clearly says it is a Syntax Error but shouldn't it even accept the statement? Why does it wait for an in input?

Thank you.

Python Version: 2.7 OS: Windows


Solution

  • var1 = int((raw_input()) has three left parentheses and two right ones. Until you complete the expression with another right parentheses, Python thinks you have not finished writing the expression. That is why it shows an ellipsis.

    When you type "12", the full expression becomes var1 = int((raw_input())12, which is not valid syntax because you can't have a number immediately after a closing paren.