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
#1
I'm using the following code:

import random

def get_data():
    colours = ["red","yellow","orange","green","blue","purple"]
    c1 = random.choice(colours)
    c2 = random.choice(colours)
    c3 = random.choice(colours)
    c4 = random.choice(colours)
    print("The colours to choose from are: red, yellow, orange, green, blue and purple")
    guess1 = input("Please enter your choice for the 1st colour: ")
    guess2 = input("Please enter your choice for the 2nd colour: ")
    guess3 = input("Please enter your choice for the 3rd colour: ")
    guess4 = input("Please enter your choice for the 4th colour: ")
    if guess1 != "red" and guess1 != "yellow" and guess1 != "orange" and guess1 != "green" and guess1 != "blue" and guess1 != "purple":
        print("Incorrect choice for guess1 please try again") # this block of code makes sure user enters colours from the list
        guess1 = input("Please enter your choice for the 1st colour: ")
    if guess2 != "red" and guess2 != "yellow" and guess2 != "orange" and guess2 != "green" and guess2 != "blue" and guess2 != "purple":
        print("Incorrect choice for guess2 please try again")
        guess2 = input("Please enter your choice for the 2nd colour: ")
    if guess3 != "red" and guess3 != "yellow" and guess3 != "orange" and guess3 != "green" and guess3 != "blue" and guess3 != "purple":
        print("Incorrect choice for guess3 please try again")
        guess3 = input("Please enter your choice for the 3rd colour: ")
    if guess4 != "red" and guess4 != "yellow" and guess4 != "orange" and guess4 != "green" and guess4 != "blue" and guess4 != "purple":
        print("Incorrect choice for guess4 please try again")
        guess4 = input("Please enter your choice for the 4th colour: ")
    data = (guess1,guess2,guess3,guess4)
    answers = (c1,c2,c3,c4)
    return data
    return answers

def quiz(data,answers):
    correct = 0 
    attempts = 0
    while correct < 4:
        answer1 = False
        while answer1 == False:
            if guess1 == c1: # if answer is correct
                print("Colour in position 1 correct") # print this message 
                correct = correct + 1 # adds 1 to correct total
                attempts = attempts + 1 # adds 1 to attempts total
                answer1 = True # closes while loop
            elif guess1 == c2 or guess1 == c3 or guess1 == c4: # if answer is correct but in wrong place
                print("Colour correct but in wrong place position 1") # prints this message
                attempts = attempts + 1 # adds 1 to attempts total
                guess1 = input("Please enter your choice for the 1st colour: ") # asks user for another guess
            else: # if answer is wrong
                print("Wrong colour chosen") # print this message 
                attempts = attempts + 1 # adds 1 to attempts total
                guess1 = input("Please enter your choice for the 1st colour: ") # asks user for another guess
        answer2 = False # code repeats this process for the other 3 answers 
        while answer2 == False:
            if guess2 == c2:
                print("Colour in position 2 correct")
                correct = correct + 1
                attempts = attempts + 1
                answer2 = True
            elif guess2 == c1 or guess1 == c3 or guess1 == c4:
                print("Colour correct but in wrong place position 2")
                attempts = attempts + 1
                guess2 = input("Please enter your choice for the 2nd colour: ")
            else:
                print("Wrong colour chosen")
                attempts = attempts + 1
                guess2 = input("Please enter your choice for the 2nd colour: ")
        answer3 = False
        while answer3 == False:
            if guess3 == c3:
                print("Colour in position 3 correct")
                correct = correct + 1
                attempts = attempts + 1
                answer3 = True
            elif guess3 == c1 or guess1 == c2 or guess1 == c4:
                print("Colour correct but in wrong place position 3")
                attempts = attempts + 1
                guess3 = input("Please enter your choice for the 3rd colour: ")
            else:
                print("Wrong colour chosen")
                attempts = attempts + 1
                guess3 = input("Please enter your choice for the 3rd colour: ")
        answer4 = False
        while answer4 == False:
            if guess4 == c4:
                print("Colour in position 2 correct")
                correct = correct + 1
                attempts = attempts + 1
                answer4 = True
            elif guess4 == c1 or guess1 == c2 or guess1 == c3:
                print("Colour correct but in wrong place position 4")
                attempts = attempts + 1
                guess2 = input("Please enter your choice for the 4th colour: ")
            else:
                print("Wrong colour chosen")
                attempts = attempts + 1
                guess4 = input("Please enter your choice for the 4th colour: ")
    print("Well done! You got all the colours right") # prints this message when all colours are correct 
    print("The number of guesses taken was: ", attempts) # tells them how many guesses they took 
    data2 = (correct,attempts) # stores these variables as data2 
    return data2 # returns data2 for use in other subprograms 

def main():
    (guess1,guess2,guess3,guess4) = get_data() # gets these variables from get_data    
    (data,answers) = quiz # runs quiz subprogram with these variables
    quiz()

main() # runs main subprogram
But I get the following error

Error:
File "c:/Users/djwil/Documents/python/learning python/Chapter 19 - Chunky Challenges/Mastermind.py", line 105, in <module> main() # runs main subprogram File "c:/Users/djwil/Documents/python/learning python/Chapter 19 - Chunky Challenges/Mastermind.py", line 102, in main (data,answers) = quiz # runs quiz subprogram with these variables TypeError: cannot unpack non-iterable function object
Why am I getting this error?
How do I fix this and avoid this in the future?
Reply
#2
quiz is a function and in Python (like lots of languages these days), functions are first class meaning that you can assign them to variables and pass them to other functions. That's what the code you have on that line is attempting to do. Instead, you need to call the function by including the parentheses after its name and passing whatever arguments it needs.
Reply
#3
Hi, firstly thanks for your reply. I think I've done what you suggested, the main subprogram is now:
def main():
    (guess1,guess2,guess3,guess4) = get_data() # gets these variables from get_data    
    quiz(data,answers)
However I'm now getting this error:

Error:
File "c:/Users/djwil/Documents/python/learning python/Chapter 19 - Chunky Challenges/Mastermind.py", line 104, in <module> main() # runs main subprogram File "c:/Users/djwil/Documents/python/learning python/Chapter 19 - Chunky Challenges/Mastermind.py", line 102, in main quiz(data,answers) NameError: name 'data' is not defined
But I have defined and returned it in the earlier subprogram get_data so why is this error occuring?
Reply
#4
there are plenty of issues in your code
on line 28 you return data and the return statement on line 29 is NEVER executed.
on line 101 you unpack value returned from get_data() into 4 variables.
data used on line 102 is never defined, as well as answers
variables defined on line 101 are never used
arguments data and answers, that are expected in quiz function are never used in the body of the function.
c1, c2... c4 used in the quiz function are never defined (you really need to read about scope)
values that is returned when you [succeed to] call quiz(data,answers), will be thrown away

these are errors in coding, but there are also errors in logic - e.g. when check answer you have separate loops for each color, i.e. user will know nothing for colors 2-4 till they succeed to guess right the color 1. I think you want to check all colors as one single answer.

you have so much repeating code, that just screams to define a function. mixing the taking of user input and the computer selection of colors in a single function is bad design
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#5
I'm using the following code:
import random

def get_data():
    colours = ["red","yellow","orange","green","blue","purple"]
    c1 = random.choice(colours)
    c2 = random.choice(colours)
    c3 = random.choice(colours)
    c4 = random.choice(colours)
    print("The colours to choose from are: red, yellow, orange, green, blue and purple")
    guess1 = input("Please enter your choice for the 1st colour: ")
    guess2 = input("Please enter your choice for the 2nd colour: ")
    guess3 = input("Please enter your choice for the 3rd colour: ")
    guess4 = input("Please enter your choice for the 4th colour: ")
    if guess1 != "red" and guess1 != "yellow" and guess1 != "orange" and guess1 != "green" and guess1 != "blue" and guess1 != "purple":
        print("Incorrect choice for guess1 please try again") # this block of code makes sure user enters colours from the list
        guess1 = input("Please enter your choice for the 1st colour: ")
    if guess2 != "red" and guess2 != "yellow" and guess2 != "orange" and guess2 != "green" and guess2 != "blue" and guess2 != "purple":
        print("Incorrect choice for guess2 please try again")
        guess2 = input("Please enter your choice for the 2nd colour: ")
    if guess3 != "red" and guess3 != "yellow" and guess3 != "orange" and guess3 != "green" and guess3 != "blue" and guess3 != "purple":
        print("Incorrect choice for guess3 please try again")
        guess3 = input("Please enter your choice for the 3rd colour: ")
    if guess4 != "red" and guess4 != "yellow" and guess4 != "orange" and guess4 != "green" and guess4 != "blue" and guess4 != "purple":
        print("Incorrect choice for guess4 please try again")
        guess4 = input("Please enter your choice for the 4th colour: ")
    data = (guess1,guess2,guess3,guess4)
    answers = (c1,c2,c3,c4)
    return data
    return answers

def quiz(data,answers):
    correct = 0 
    attempts = 0
    while correct < 4:
        answer1 = False
        while answer1 == False:
            if guess1 == c1: # if answer is correct
                print("Colour in position 1 correct") # print this message 
                correct = correct + 1 # adds 1 to correct total
                attempts = attempts + 1 # adds 1 to attempts total
                answer1 = True # closes while loop
            elif guess1 == c2 or guess1 == c3 or guess1 == c4: # if answer is correct but in wrong place
                print("Colour correct but in wrong place position 1") # prints this message
                attempts = attempts + 1 # adds 1 to attempts total
                guess1 = input("Please enter your choice for the 1st colour: ") # asks user for another guess
            else: # if answer is wrong
                print("Wrong colour chosen") # print this message 
                attempts = attempts + 1 # adds 1 to attempts total
                guess1 = input("Please enter your choice for the 1st colour: ") # asks user for another guess
        answer2 = False # code repeats this process for the other 3 answers 
        while answer2 == False:
            if guess2 == c2:
                print("Colour in position 2 correct")
                correct = correct + 1
                attempts = attempts + 1
                answer2 = True
            elif guess2 == c1 or guess1 == c3 or guess1 == c4:
                print("Colour correct but in wrong place position 2")
                attempts = attempts + 1
                guess2 = input("Please enter your choice for the 2nd colour: ")
            else:
                print("Wrong colour chosen")
                attempts = attempts + 1
                guess2 = input("Please enter your choice for the 2nd colour: ")
        answer3 = False
        while answer3 == False:
            if guess3 == c3:
                print("Colour in position 3 correct")
                correct = correct + 1
                attempts = attempts + 1
                answer3 = True
            elif guess3 == c1 or guess1 == c2 or guess1 == c4:
                print("Colour correct but in wrong place position 3")
                attempts = attempts + 1
                guess3 = input("Please enter your choice for the 3rd colour: ")
            else:
                print("Wrong colour chosen")
                attempts = attempts + 1
                guess3 = input("Please enter your choice for the 3rd colour: ")
        answer4 = False
        while answer4 == False:
            if guess4 == c4:
                print("Colour in position 2 correct")
                correct = correct + 1
                attempts = attempts + 1
                answer4 = True
            elif guess4 == c1 or guess1 == c2 or guess1 == c3:
                print("Colour correct but in wrong place position 4")
                attempts = attempts + 1
                guess2 = input("Please enter your choice for the 4th colour: ")
            else:
                print("Wrong colour chosen")
                attempts = attempts + 1
                guess4 = input("Please enter your choice for the 4th colour: ")
    print("Well done! You got all the colours right") # prints this message when all colours are correct 
    print("The number of guesses taken was: ", attempts) # tells them how many guesses they took 
    data2 = (correct,attempts) # stores these variables as data2 
    return data2 # returns data2 for use in other subprograms 

def main():
    (data,answers) = get_data() # gets these variables from get_data    
    quiz(data,answers)

main() # runs main subprogram
But I'm getting this error:

Error:
File "c:/Users/djwil/Documents/python/learning python/Chapter 19 - Chunky Challenges/Mastermind.py", line 104, in <module> main() # runs main subprogram File "c:/Users/djwil/Documents/python/learning python/Chapter 19 - Chunky Challenges/Mastermind.py", line 101, in main (data,answers) = get_data() # gets these variables from get_data ValueError: too many values to unpack (expected 2)
How should I go about getting all the values I need into my main subprogram?
Reply
#6
At the end of the function get_data
change
    return data
    return answers
to
    return data, answers
Reply
#7
Of course, the second line isn't even executed because as soon as you encounter a return statement, the function returns.
Reply
#8
I've merged the new thread you started into old one. I've already pointed out number of issues with your code
I don't see the point of starting new thread for every one of them, they are more or less connected, e.g. data and answers arguments are not used inside quiz function and the next errors you will get is that c1, c2, c3 and c4 are not defined.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#9
But the variables contained in data and answer are used in the quiz, so by having :

def quiz(data,answers):
shouldn't that allow me to use those variables in the subprogram?
Reply
#10
yes, they are defined in the function signature as parameters you expect. However, they are not used in the body of the said function (or at least what you have show us):
correct = 0 
    attempts = 0
    while correct < 4:
        answer1 = False
        while answer1 == False:
            if guess1 == c1: # if answer is correct
                print("Colour in position 1 correct") # print this message 
                correct = correct + 1 # adds 1 to correct total
                attempts = attempts + 1 # adds 1 to attempts total
                answer1 = True # closes while loop
            elif guess1 == c2 or guess1 == c3 or guess1 == c4: # if answer is correct but in wrong place
                print("Colour correct but in wrong place position 1") # prints this message
                attempts = attempts + 1 # adds 1 to attempts total
                guess1 = input("Please enter your choice for the 1st colour: ") # asks user for another guess
            else: # if answer is wrong
                print("Wrong colour chosen") # print this message 
                attempts = attempts + 1 # adds 1 to attempts total
                guess1 = input("Please enter your choice for the 1st colour: ") # asks user for another guess
        answer2 = False # code repeats this process for the other 3 answers 
        while answer2 == False:
            if guess2 == c2:
                print("Colour in position 2 correct")
                correct = correct + 1
                attempts = attempts + 1
                answer2 = True
            elif guess2 == c1 or guess1 == c3 or guess1 == c4:
                print("Colour correct but in wrong place position 2")
                attempts = attempts + 1
                guess2 = input("Please enter your choice for the 2nd colour: ")
            else:
                print("Wrong colour chosen")
                attempts = attempts + 1
                guess2 = input("Please enter your choice for the 2nd colour: ")
        answer3 = False
        while answer3 == False:
            if guess3 == c3:
                print("Colour in position 3 correct")
                correct = correct + 1
                attempts = attempts + 1
                answer3 = True
            elif guess3 == c1 or guess1 == c2 or guess1 == c4:
                print("Colour correct but in wrong place position 3")
                attempts = attempts + 1
                guess3 = input("Please enter your choice for the 3rd colour: ")
            else:
                print("Wrong colour chosen")
                attempts = attempts + 1
                guess3 = input("Please enter your choice for the 3rd colour: ")
        answer4 = False
        while answer4 == False:
            if guess4 == c4:
                print("Colour in position 2 correct")
                correct = correct + 1
                attempts = attempts + 1
                answer4 = True
            elif guess4 == c1 or guess1 == c2 or guess1 == c3:
                print("Colour correct but in wrong place position 4")
                attempts = attempts + 1
                guess2 = input("Please enter your choice for the 4th colour: ")
            else:
                print("Wrong colour chosen")
                attempts = attempts + 1
                guess4 = input("Please enter your choice for the 4th colour: ")
    print("Well done! You got all the colours right") # prints this message when all colours are correct 
    print("The number of guesses taken was: ", attempts) # tells them how many guesses they took 
    data2 = (correct,attempts) # stores these variables as data2 
    return data2 # returns data2 for use in other subprograms 
so, you take 2 arguments and just discard them.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Too much values to unpack actualpy 3 472 Feb-11-2024, 05:38 PM
Last Post: deanhystad
  Need help with 'str' object is not callable error. Fare 4 829 Jul-23-2023, 02:25 PM
Last Post: Fare
  cx_oracle Error - AttributeError: 'function' object has no attribute 'cursor' birajdarmm 1 2,360 Apr-15-2023, 05:17 PM
Last Post: deanhystad
  Error in Int object is not subscript-able. kakut 2 1,180 Jul-06-2022, 08:31 AM
Last Post: ibreeden
Question how to solve `'TypeError: 'int' object is not iterable`? netanelst 2 1,572 May-24-2022, 12:03 PM
Last Post: deanhystad
  unpack dict menator01 1 1,193 Apr-09-2022, 03:10 PM
Last Post: menator01
  AttributeError: 'function' object has no attribute 'metadata 3lnyn0 5 4,617 Mar-28-2022, 04:42 PM
Last Post: Larz60+
  ValueError: not enough values to unpack (expected 4, got 1) vlearner 2 6,328 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,826 Sep-21-2021, 08:32 PM
Last Post: 1968Edwards
  Trying to understand how isinstance(values, collections.Iterable) work. quazirfan 7 4,195 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