Python Forum
Return Statement in Python IDLE editor
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Return Statement in Python IDLE editor
#1
Hi, I'm somewhat of a programming "newbie" , so bear with me.

I have been practising with Python (2) through Python IDLE. However, when I type code in the editor window and utilize a return statement, the return statement doesn't work after I run the code (there's no output in the shell window). Normally I would be able to type my code in the editor window, run it,and then get the results in the shell window.

However, when I type that same code in the shell window, and run the code, the return statement does work. How do I solve this? I want to be able to just type my code in editor mode, and then run.


def compare(x,y):
       if x > y:
               return 1
       if x == y:
               return 0
       if x < y:
               return -1

compare(5,7)
Reply
#2
Idle is not the best editor, it has some quirks.
You'd be much better off choosing another editor.
Though choice of an IDE is a personal preference, I use and like PyCharm (community edition)
Others on this site praise atom editor
Reply
#3
(Jul-11-2017, 07:32 PM)Larz60+ Wrote: Idle is not the best editor, it has some quirks.
You'd be much better off choosing another editor.
Though choice of an IDE is a personal preference, I use and like PyCharm (community edition)
Others on this site praise atom editor

Thanks, I'll try PyCharm..:-)
Reply
#4
In IDLE(file code window(run F5) or other editors you need to print() to get the result.
REPL interactive interpreter(>>>) dos not need print().
>>> a = 'hello'
>>> a
'hello'
As code to run.
a = 'hello'
print(a)
Use 4-space indentation.
IPython or ptpython in combination with cmder,
you get much better REPL and nice colors that cmd dos not have.
[Image: vrwOsD.jpg]
Reply
#5
(Jul-11-2017, 08:14 PM)snippsat Wrote: In IDLE(file code window(run F5) or other editors you need to print() to get the result.
REPL interactive interpreter(>>>) dos not need print().

Thanks, I did indeed download and install Pycharm like the other member suggested, but no luck using that either. So I guess I'll just have to use the interpreter/shell mode to see the results of my return statement.

By the way, is there any Python GUI that allows you to run part of the code by either selecting the part you want to run and then choosing run selection or something?
Reply
#6
(Jul-11-2017, 06:44 PM)NMW Wrote: Hi, I'm somewhat of a programming "newbie"

As a "newbie" you should really be using the most recent version of Python, currently 3.6.1 (due to be updated to 3.6.2 on 17 July 2017) unless you absolutely need to use version 2
If it ain't broke, I just haven't gotten to it yet.
OS: Windows 10, openSuse 42.3, freeBSD 11, Raspian "Stretch"
Python 3.6.5, IDE: PyCharm 2018 Community Edition
Reply
#7
Quote:Thanks, I did indeed download and install Pycharm like the other member suggested, but no luck using that either. So I guess I'll just have to use the interpreter/shell mode to see the results of my return statement.
Did you understand the part with print()?
You download PyCharm community edition(free).
[Image: JspzSU.jpg]
Quote:By the way, is there any Python GUI that allows you to run part of the code by either selecting the part you want to run and then choosing run selection or something?
Yes, an PyCharm has a couple of ways,when i use it sometimes(i use Atom).
I use mark code --> right click --> Execute selection in console.
The get output and can continue to test.
If mark all code i get -1,but can continue to to test.
-1
>>> compare(7, 4)
1
>>> compare(7, 7)
0
PyCharm is good for beginner because you get all tool out of the box(use basic tool to run code in the beginning).
Reply
#8
(Jul-11-2017, 09:21 PM)sparkz_alot Wrote:
(Jul-11-2017, 06:44 PM)NMW Wrote: Hi, I'm somewhat of a programming "newbie"

As a "newbie" you should really be using the most recent version of Python, currently 3.6.1 (due to be updated to 3.6.2 on 17 July 2017) unless you absolutely need to use version 2

I don't know, isn't there some kind of age old discussion as to which Python version (2 or 3) you should use if you want to learn Python? And "experts" have stated that if you're in the learning stages you should use Python 2 ?
and I quote :
Quote:“Python 3 is a nicer and more consistent language, BUT, there is very limited third-party module support for it. This is likely to be true for at least a couple of years more. So, all major frameworks still run on Python 2, and will continue to do so for a significant time. Therefore, if you learn Python today, you should learn Python 2, because that is the version you are going to end up actually using.”
source:http://learntocodewith.me/programming/python/python-2-vs-python-3/

That's why I'm using Python 2, I guess...

Quote:Did you understand the part with print()?

Yes I did, I could adapt my code or I could just use the interpreter. Both would work, I guess. I was afraid that my code was wrong, but now I know it's probably just some sort of "glitch"...

(Jul-11-2017, 09:30 PM)snippsat Wrote:
Quote:Yes, an PyCharm has a couple of ways,when i use it sometimes(i use Atom).

Quote:PyCharm is good for beginner because you get all tool out of the box(use basic tool to run code in the beginning).

Thanks, I'll use PyCharm for my next Python tutorials... :-)
Reply
#9
(Jul-11-2017, 09:31 PM)NMW Wrote: source:http://learntocodewith.me/programming/python/python-2-vs-python-3/
That's from 2014 and do not make sense now.
All beginners should learn Python 3,and start with Python 3.6(install tutorial)
If you look further down the post.
Quote:The StackOverflow discussion posted above has been updated
to reflect that one SHOULD start with Python 3 if they are learning today, in 2015.
Reply
#10
Just an FYI
Quote:but no luck using that either
this in pycharm:
def compare(x, y):
   if x > y:
       return 1
   if x == y:
       return 0
   if x < y:
       return -1
print(compare(5, 7))
Output:
-1 Process finished with exit code 0
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Why can I not see the code correctly in Python IDLE.? Trump 7 434 Mar-14-2024, 09:05 PM
Last Post: deanhystad
  Launch Python IDLE Shell from terminal Pavel_47 5 1,143 Feb-17-2023, 02:53 PM
Last Post: Pavel_47
  Python Idle won't start totalmachine 9 3,382 Oct-16-2022, 05:57 PM
Last Post: totalmachine
  Why is IDLE not opening on a MacOS Montery 12.6 and using Python 3.10.7? Merlin385 7 1,592 Oct-08-2022, 08:36 PM
Last Post: Merlin385
  Need help with Return statement Columbo 13 2,179 Sep-17-2022, 04:03 PM
Last Post: Columbo
  Sublime Text Editor not recognizing Python elroberto 5 2,802 Jun-13-2022, 04:00 PM
Last Post: rob101
  Music Notation editor; how to build the editor? direction to go? philipbergwerf 1 1,650 Jan-01-2022, 04:56 PM
Last Post: Larz60+
  MAC Python IDLE issue shadow12 4 2,461 Oct-29-2021, 12:22 AM
Last Post: shadow12
  Python MYSQL connection does not work after 1h idle zazas321 9 6,620 Oct-07-2021, 12:02 PM
Last Post: ndc85430
  How to invoke a function with return statement in list comprehension? maiya 4 2,752 Jul-17-2021, 04:30 PM
Last Post: maiya

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020