Python Forum
how to use the return function
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
how to use the return function
#1
Again. Trying to teach myself how to use the return function. This is me trying to get a user to make a choice between 1-5 and then returning their choice. I don’t know now returns work but if someone can help me tackle this I’d greatly appreciate it.
print("PRACTICE.")

def get_choice():
  choice = -1
  while choice < 1 or choice > 5: 
    choice = int(input("Please make a choice (1-5): "))
  return choice
Reply
#2
As per your other thread, the function works fine for me, are you calling it (get_choice())?
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
(Jan-05-2019, 11:44 PM)ichabod801 Wrote: As per your other thread, the function works fine for me, are you calling it (get_choice())?

Hello. Yes I am calling it get_choice() after the Return choice line. However, it just stops running after the user inputs a number between 1-5. Is that supposed to happen?
Reply
#4
Yeah, that's what it should do. It's designed to get a choice from 1-5. Once it gets that value, it returns it and stops.

Are you expecting it to do something else?
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#5
(Jan-07-2019, 05:34 PM)ichabod801 Wrote: Yeah, that's what it should do. It's designed to get a choice from 1-5. Once it gets that value, it returns it and stops. Are you expecting it to do something else?

Only it didn't return anything

Output:
PRACTICE.
Please make a choice(1-5): 2 (Entered by me)
>
Reply
#6
How are you running that? If you run it from the system command line, you won't see the return value. If you run it in the Python interpreter you should see the output. This is what I get in the interpreter:

Output:
>>> get_choice() Please make a choice (1-5): 3 3 >>>
Also note that you have to call it from within the interpreter to see it in the interpreter. In any case, if you change the call to print(get_choice()) you'll see the return value.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  nested function return MHGhonaim 2 613 Oct-02-2023, 09:21 AM
Last Post: deanhystad
  return next item each time a function is executed User3000 19 2,283 Aug-06-2023, 02:29 PM
Last Post: deanhystad
  function return boolean based on GPIO pin reading caslor 2 1,183 Feb-04-2023, 12:30 PM
Last Post: caslor
  return vs. print in nested function example Mark17 4 1,745 Jan-04-2022, 06:02 PM
Last Post: jefsummers
  How to invoke a function with return statement in list comprehension? maiya 4 2,844 Jul-17-2021, 04:30 PM
Last Post: maiya
  Function - Return multiple values tester_V 10 4,450 Jun-02-2021, 05:34 AM
Last Post: tester_V
  Get return value from a threaded function Reverend_Jim 3 17,100 Mar-12-2021, 03:44 AM
Last Post: Reverend_Jim
  Return not exiting function?? rudihammad 3 5,296 Dec-01-2020, 07:11 PM
Last Post: bowlofred
  Why does my function return None? vg100h 3 2,208 Oct-29-2020, 06:17 AM
Last Post: vg100h
  how to keep a Popen instance existant in a function return? Skaperen 7 3,161 Sep-17-2020, 07:10 PM
Last Post: Skaperen

Forum Jump:

User Panel Messages

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