Python Forum
subprogram issues: cannot unpack non-iterable function object error
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
subprogram issues: cannot unpack non-iterable function object error
#11
Those are parameters to the function and so the names are only relevant inside the function. The variables you pass to the function containing the values for those parameters of course don't need to have the same names as those. It wouldn't really make sense to enforce that, would it?

For example, given

>>> def foo(x, y):
...     print(x, y)
... 
I can call all of these:

>>> z = 1
>>> t = 2
>>> foo(z, t)
1 2
>>> foo(3, 4)
3 4
>>> x = 5
>>> y = 10
>>> foo(x, y)
5 10
>>> 
Note that in the last example, there are two variables x: one local to the function and one outside it. Perhaps you need to learn about scope if you don't understand this?
Reply
#12
How should I call the variables so that they can be used in different subprograms?
I'm currently getting this error after changing the block of code to Yoriz's suggestion:

Error:
File "c:/Users/djwil/Documents/python/learning python/Chapter 19 - Chunky Challenges/Mastermind.py", line 103, in <module> main() # runs main subprogram File "c:/Users/djwil/Documents/python/learning python/Chapter 19 - Chunky Challenges/Mastermind.py", line 101, in main quiz(data,answers) File "c:/Users/djwil/Documents/python/learning python/Chapter 19 - Chunky Challenges/Mastermind.py", line 36, in quiz if guess1 == c1: # if answer is correct UnboundLocalError: local variable 'guess1' referenced before assignment

I've read up on global and local scope in python, I'm not sure how to call the data from the other subprogram where it has local scope into another subprogram, without defining it outside of the subprograms which would give it global scope.
Reply
#13
You have returned
data = (guess1,guess2,guess3,guess4)
answers = (c1,c2,c3,c4)
from the function get_data
You pass those in to quiz(data,answers)
so
if guess1 == c1:
should actually be
if data[0] == answers[0]:
to obtain the values at index zero of both the tuples.
Reply
#14
I think your program would look a lot better if you got rid of all variables named choiceN or cN where N is a number from 1 to 4. Variable names that look the same except for a numeric suffix are a good indication that you should be using a collection instead. Since order is important I would use a sequence; list of tuple. That will take care of most of your variable passing issues.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Too much values to unpack actualpy 3 460 Feb-11-2024, 05:38 PM
Last Post: deanhystad
  Need help with 'str' object is not callable error. Fare 4 825 Jul-23-2023, 02:25 PM
Last Post: Fare
  cx_oracle Error - AttributeError: 'function' object has no attribute 'cursor' birajdarmm 1 2,325 Apr-15-2023, 05:17 PM
Last Post: deanhystad
  Error in Int object is not subscript-able. kakut 2 1,174 Jul-06-2022, 08:31 AM
Last Post: ibreeden
Question how to solve `'TypeError: 'int' object is not iterable`? netanelst 2 1,561 May-24-2022, 12:03 PM
Last Post: deanhystad
  unpack dict menator01 1 1,184 Apr-09-2022, 03:10 PM
Last Post: menator01
  AttributeError: 'function' object has no attribute 'metadata 3lnyn0 5 4,595 Mar-28-2022, 04:42 PM
Last Post: Larz60+
  ValueError: not enough values to unpack (expected 4, got 1) vlearner 2 6,324 Jan-28-2022, 06:36 PM
Last Post: deanhystad
  Conjugate Gradient having issues with defining A (function to solve [A]{x} = {b} ) DimosG 2 2,821 Sep-21-2021, 08:32 PM
Last Post: 1968Edwards
  Trying to understand how isinstance(values, collections.Iterable) work. quazirfan 7 4,179 Aug-10-2021, 08:10 AM
Last Post: snippsat

Forum Jump:

User Panel Messages

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