2

I'll want to know how to call a function in vs code. I read the answer to similar questions, but they don't work:

def userInput(n):
    return n*n
userInput(5)

And appends nothing

def Input(n):
    return n*n

And in the terminal:

from file import *
from: can't read /var/mail/file

Can somebody help me?

5
  • 1
    I don't understand what is wrong with the first snippet. You seem to have defined a function and called it. Did you want to print the return value? Commented Apr 13, 2021 at 15:50
  • 1
    Please don't think I'm being rude, but I think you should look for a basic introduction to the Python programming language. Commented Apr 13, 2021 at 15:53
  • Is the problem that you want to see the output? If so you have to print it like @quamrana said. Commented Apr 13, 2021 at 15:53
  • python -u "/home/eugenio/.config/sublime-text-3/Packages/User/file.py" Commented Apr 13, 2021 at 15:58
  • @Nicholas Hunter that's not the problem Commented Apr 13, 2021 at 16:01

3 Answers 3

1

You are doing everything correctly in the first picture. In order to call a function in python on vs code you first have to define the function, which you did by typing def userInput(n):. If you want to see the result of your function, you should not use return, you should use print instead. Return is a keyword- so when your computer reaches the return keyword it attempts to send that value from one point in your code to another. If you want to see the result of your code, typing print (n) would work better.

Your code should look like this:

def userInput(n):
    print (n * n)
userInput(5)

The code would print the result 25

Sign up to request clarification or add additional context in comments.

1 Comment

Of course. Here is an article about the difference, it can get confusing sometimes. pythonprinciples.com/blog/print-vs-return
1

Really late to the game but if you have the Python extension, see bottom link if you don't have it. Now you can do a few things in VS to make it easier for reuse:

  1. Just go into you code file on the browser of folders in VS Code.

  2. Open your file.

  3. Highlight in the file itself only what you want to run in this case:

    def userInput(n):
        return n*n
    
  4. Press Shift+Enter

  5. You will see your code go into an interpreter like below. Now I can recall function 'userInput(n)' over and over and over and over again how I like. enter image description here

More tips on extension are here too: https://code.visualstudio.com/docs/python/python-quick-start

1 Comment

This works well. Additional Suggestion: If you have an import statement and/or are using a constant value, you'll need to include those statements at the prompt. I had to do this in order for my function to work properly.
0

Your terminal is your general way to access your operating system, so you have to tell it that you want it to interpret your Python code first.

If you want to run the file you're typing in, you have to first know the location of that file. When you type ls in your terminal, does the name of your Python file show up? If not, hover over the tab in VSCode (it's close to the top of the editor) and see what path appears. Then in your terminal type cd (short for "change directory") and then the path that you saw, minus the <your filename here>.py bit. Type ls again, and you should see your Python file. Now you can type python <your filename here>.py to run it (provided you have Python installed).

You could also run the IDLE by just typing python in your terminal. This will allow you to write your code line-by-line and immediately evaluate it, but it's easier to write in VSCode and then run it with the method I described before.

3 Comments

It says: cd -file.py bash: cd: -f: invalid option cd: usage: cd [-L|[-P [-e]] [-@]] [dir]
And to open IDLE you have to type idle by the way
@linux file.py is your filename here, you want the directory where it's in. maybe your terminal is already at the correct folder, try doing python file.py in the terminal?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.