Difference between running python codes in visual studio

Hi Folks, I am trying to run python code by pressing shift enter, and another way of running the python command is using “Run python file” button. I am running the below code:
print(“How old are you?”, end=’ ')

age = input()

print(“How tall are you?”, end=’ ')

height = input()

print(“How much do you weigh?”, end=’ ')

weight = input()

print(f"So, you’re {age} old, {height} tall and {weight} heavy")

so when I am pressing shift enter I am getting the below output -

print(“How old are you?”, end=’ ‘)
How old are you? >>> age = input()
print(“How tall are you?”, end=’ ‘)
height = input()
print(“How much do you weigh?”, end=’ ')
weight = input()
print(f"So, you’re {age} old, {height} tall and {weight} heavy")

and when I am pressing the using “Run python file” button then I am getting the below output

yugalgarg@Yugals-MacBook-Pro ~ % /usr/local/bin/python3 “/Users/yugalgarg/Desktop/Python Exercise/ex11.py”
How old are you? 25
How tall are you? 5.11 inch
How much do you weigh? 77 kg
So, you’re 25 old, 5.11 inch tall and 77 kg heav

can anyone please tell me what’s something I am missing here and what is the difference between when I am running the code pressing the button and when I am running the code pressing shift enter?

According to the reference material, Shift+Enter is “Find Previous” so I don’t think that’s what’s going on… you might want to check the Command Palette for what the configured shortcut for “run program” is.

2 Likes

Please read the pinned thread in order to understand how to format code and terminal output properly for posting, so that we can properly see and understand what you are seeing.

1 Like

Additional digging led me to a different binding for Shift+Enter:

Python: Run Selection/Line in Python Terminal

so that appears to be the issue, you’re running your code in a quasi-interactive mode instead of as a script.

‘’'python
print(“How old are you?”, end=’ ‘)
How old are you? >>> age = input()
print(“How tall are you?”, end=’ ‘)
height = input()
print(“How much do you weigh?”, end=’ ‘)
weight = input()
print(f"So, you’re {age} old, {height} tall and {weight} heavy")
‘’’

Output (When pressing shift enter)
print(“How old are you?”, end=’ ')
age = input()
print(“How tall are you?”, end=’ ')
height = input()
print(“How much do you weigh?”, end=’ ')
weight = input()
print(f"So, you’re {age} old, {height} tall and {weight} heavy")

Output (pressing the using “Run python file” button)
yugalgarg@Yugals-MacBook-Pro ~ % /usr/local/bin/python3 “/Users/yugalgarg/Desktop/Python Exercise/ex11.py”
How old are you? 25
How tall are you? 5.11 inch
How much do you weigh? 77 kg
So, you’re 25 old, 5.11 inch tall and 77 kg heav

Please note that I have given inputs as 25, 5.11 inch, 77 kg

Please let me know if my post is in perfect shape now.