Search code examples
pythonvisual-studio-codeinputcasting

Why outputs differ between VScode python terminal & Interactive window?


I was runing this code on VScode :

a = input("a : ")
b = int(a) + 2
print(f"a:{a},b:{b}")

The output on Python terminal:

a : b = int(a) + 2
>>> print(f"a:{a},b:{b}")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'b' is not defined 

The Output on Interactive window for input 12 :

 a:12,b:14

What's the problem here ?

P.S. A similar terminal problem is in this thread but still without any solution. Why there are different outputs here?


Solution

  • Thanks to everyone for the help. What I found out from the answers is that Terminal & Interactive window behave different from each other.

    Terminal : Executes line by line of code. So giving a block of code at once with input at first line of the block doesn't work well here.

    Interactive window : Can take blocks of code & act/execute smarter than the Terminal as you can see in the output of the question.