Return does not return anything in Spyder. It works fine with other IDE

2024/10/5 15:16:18

I just moved to spyder for Python and the return function doesn't seem to work:

def test():return 2
test()

The IPython console is empty. If I use print instead of return it works fine. Any idea?

I use python 3.7 with Spyder 4.1.5

Thanks in advance!

Answer

The editor is handling your code as a normal python script which means statements ala
test() don't print their result. Instead you will need to do something with the value you are returning E.g. assign it to a variable or print it.

That you get the result of something displayed immediately is a special function of the python console and not normal python behaviour.

To actually print the result you have to call print(test()) so try this:

 def test():
return 2
enter    >>press enter
print(test()>>press enter 
https://en.xdnf.cn/q/120181.html

Related Q&A

Error in goto module [Python]

Ok, let me start by saying I know that it is bad that I am using the goto module and I shouldnt be and blah blah blah. However, for this specific purpose I need it. Let me also say that I am new to Pyt…

How to scrape all product review from lazada in python

i currently working on web scraping of data from the lazada site using selenium in python: https://www.lazada.sg/products/loreal-paris-uv-perfect-even-complexion-sunscreen-spf50pa-30ml-i214861100-s325…

How to compare 2 successive row values in a resultset object using python

I have a table issue_logs:id | issue_id | from_status | to_status | up_date | remarks ----+----------+-------------+-----------+----------------------------------+----------…

Getting all possible combination for [1,0] with length 3 [0,0,0] to [1,1,1]

from itertools import combinationsdef n_length_combo(arr, n):# using set to deal# with duplicates return list(combinations(arr, n))# Driver Function if __name__ == "__main__":arr = 01n = 3pri…

Compare values under multiple conditions of one column in Python

I have the following data:data = {"index": [1, 2, 3, 4, 5],"name": ["A", "A", "B", "B", "B"],"type": [s1, s2, s1, s2, s3]…

Python: Tkinter :Dynamically Create Label

I am trying to create Label Dynamically , I am getting invalid Syntax. Can you please help me what i am missing or any alternativecrsr = cnxn.execute(query)row_num=2column_num=0Variable_Number=1for row…

TypeError: str object is not callable when trying to click datepicker

The relevant HTML<div id="datepickerbox" class="ym-gbox-left"><div class="datepick_label"><div id="datepicker" class="hasDatepicker">…

Stanford parser with NLTK produces empty output

I am trying to use the Stanford parser in a small application written in Python with the NLTK interface. I tried the code given below.Everything seems to work right, no errors, Java is launched but I s…

How do you return a list of the matched item in string with regex? [duplicate]

This question already has answers here:Regular expression to match a dot [duplicate](8 answers)Closed 3 years ago.I made this simple functions that searches for emails in the source code of a page , th…

Indentation Error [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable…