Python Forum
Thread Rating:
  • 2 Vote(s) - 1.5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
return issue in a function
#1
I am trying to print the list at the end of my code. But I can't seem to figure out how to return the list when it is called inside another function.

[code]def main():
    select()
    print(list1, list2)
def select():
    select = 0
    select = int(input('Select one of the following: \n'
                   '(1) for list one \n'
                   '(2) for list two'))
    try:
        if select == 1:
            one()
        elif select == 2:
            two()
    except ValueError:
        print('try again')
def one():
    list1 = []
    list1.append(1)
    return list1
def two():
    list2 = []
    list2.append(1)
    list2.append(2)
    return list2
def print(list1, list2):
    print(list1)
    print(list2)
main()[/code]
Reply
#2
return one()
should help
Test everything in a Python shell (iPython, Azure Notebook, etc.)
  • Someone gave you an advice you liked? Test it - maybe the advice was actually bad.
  • Someone gave you an advice you think is bad? Test it before arguing - maybe it was good.
  • You posted a claim that something you did not test works? Be prepared to eat your hat.
Reply
#3
Hello!
I don't see how this will work.
Top to the bottom.
You don't have to initialise 'select' to 0.
The input() have to be in try/except statement too. This will cause the error, not the if statement
Do not rewrite the Python's built-in functions as you did it with print(). And is meaningless because all the function does is to print something. Just like the real one. Here is a list with these built-ins.
How this rewritten print function gets its arguments. They are not global variables. This will throw a NameError.
As I see it you never create the second list because of that if statement.
main() should be defined like this
def main():
    list1, list2 = select() # but you will get "ValueError: not enough values to unpack" here
    print(list1, list2)
So the select() function has to be changed too. To return two objects. If one is populated the other could be None.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply


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