Python Forum
How to return multiple values from function in python
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to return multiple values from function in python
#1
def abc(x, y):
    print x
    print y
    x = x + 1
    y = y + 1

x = 0
y = 1
x, y = abc(x,y)
When I execute this error, I got this error: TypeError: 'NoneType' object is not iterable.

What is the reason for this? Huh
Reply
#2
Your function does not modify the global x and y.

You need to return the values.
def abc(x, y):
    x += 1
    y += 1
    return x, y
 
x = 0
y = 1
print("Before: {} {}".format(x, y))
x, y = abc(x,y)
print("After: {} {}".format(x, y))
Reply
#3
Thank you Smile
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Return not exiting function?? rudihammad 3 171 Dec-01-2020, 07:11 PM
Last Post: bowlofred
  Why does my function return None? vg100h 3 243 Oct-29-2020, 06:17 AM
Last Post: vg100h
  how to keep a Popen instance existant in a function return? Skaperen 7 418 Sep-17-2020, 07:10 PM
Last Post: Skaperen
  Looking for help in Parse multiple XMLs and update key node values and generate Out.. rajesh3383 0 249 Sep-15-2020, 01:42 PM
Last Post: rajesh3383
  Function will not return variable that I think is defined Oldman45 6 537 Aug-18-2020, 08:50 PM
Last Post: deanhystad
  print function help percentage and slash (multiple variables) leodavinci1990 3 455 Aug-10-2020, 02:51 AM
Last Post: bowlofred
  Assigning multiple values using tuple sivacg 2 366 Aug-06-2020, 10:29 PM
Last Post: perfringo
  Learning python SyntaxError: 'return' outside function Grale1953 3 444 Aug-03-2020, 06:55 AM
Last Post: buran
  Function parameters and values as string infobound 1 317 Jul-24-2020, 04:28 AM
Last Post: scidam
  How to pass multiple arguments into function Mekala 4 593 Jul-11-2020, 07:03 AM
Last Post: Mekala

Forum Jump:

User Panel Messages

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