Why is my function not working in vscode when it works in jupyternote book?

Question:

I am new to python and have recently started to study it full-time. I have been using vscode to type out my code I would run the script and it would show my output in the terminal window.

However, I cannot get a lot of the functions I create to display an output in the terminal window. However, as a comparison check, I ran the same code in a Jupyter notebook and it worked perfectly fine. I also tried running this code on another laptop in vscode running it as a python file. But nothing has worked.

Here is the code running as a python file in vscode:

Running as a python file

Here is the code running in a jupyter notebook in vscode:

Running in a jupyter notebook

I apologise if I have been unclear. Do feel free to ask any questions to get a better understanding. I am a noob at this, so any help would be really encouraging. Thanks 🙂

Asked By: addobaddo

||

Answers:

Jupyter display in the console the value of the last line of code in a cell.

In normal Python scripts this does not happen. If you want to see the output in the console you need to use the print() function or similar.

so in your case:

print(capitalize_letters("mcdonald"))

or

capitalized_word = capitalize_letters("mcdonald")
print(capitalized_word)
Answered By: f-starace

If you want to get a value from a function and use it elsewhere, you should return that value at the end of the function definition. That way when you call the function at some point in your code and you can assign it to a variable which would store the output of the function.
So you would need to use

print(capitalize_letters("macdonald"))
Answered By: NotAProgrammer