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
  __init__() got multiple values for argument 'schema' dawid294 4 2,262 Jan-03-2024, 09:42 AM
Last Post: buran
  python convert multiple files to multiple lists MCL169 6 1,533 Nov-25-2023, 05:31 AM
Last Post: Iqratech
  Need to return 2 values from 1 DF that equals another DF cubangt 5 637 Oct-21-2023, 02:45 PM
Last Post: deanhystad
  nested function return MHGhonaim 2 608 Oct-02-2023, 09:21 AM
Last Post: deanhystad
  return next item each time a function is executed User3000 19 2,276 Aug-06-2023, 02:29 PM
Last Post: deanhystad
  function return boolean based on GPIO pin reading caslor 2 1,170 Feb-04-2023, 12:30 PM
Last Post: caslor
  Adding values with reduce() function from the list of tuples kinimod 10 2,632 Jan-24-2023, 08:22 AM
Last Post: perfringo
  [Solved]Return values from npyscreen Extra 2 1,148 Oct-09-2022, 07:19 PM
Last Post: Extra
  Parallelism with return values Plexian 7 1,490 Aug-14-2022, 09:33 AM
Last Post: Plexian
  How to combine multiple column values into 1? cubangt 15 2,809 Aug-11-2022, 08:25 PM
Last Post: cubangt

Forum Jump:

User Panel Messages

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