Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Puzzling error
#1
Hi Guys

Hope you can help me with an error I don't understand.
I have the following code snippet which throws an error I don't understand:

#Class definition of an object
class Agent():
    #Constructor for the class
    def __init__(self):
        self.percepts = []
        self.action = 'NoOp'
        
   #a function which is supposed to take a tuple, use it as a dict key to look up the associated value  
   def agentprogram(percept:tuple):
        
        table = {((('A','Clean'),('B','Clean')),'A'):'NoOp',
         ((('A','Clean'),('B','Clean')),'B'):'NoOp',
         ((('A','Clean'),('B','Dirty')),'A'):'Right',
         ((('A','Clean'),('B','Dirty')),'B'):'Suck',
         ((('A','Dirty'),('B','Clean')),'A'):'Suck',
         ((('A','Dirty'),('B','Clean')),'B'):'Left',
         ((('A','Dirty'),('B','Dirty')),'A'):'Suck',
         ((('A','Dirty'),('B','Dirty')),'B'):'Suck',
        }
        
        action = table[percept]
        return action

#creating the tuple I will pass
agent_percept = ((('A','Clean'),('B','Dirty')),'B')

#instantiating an object of the class Agent
agent= Agent()

#passing the tuple and expect to get an action in return
result = agent.agentprogram(agent_percept)
This last line throws the following error:
Error:
TypeError: agentprogram() takes 1 positional argument but 2 were given
I don't get it. I'm passing a single tuple as the function argument. Why is the compiler saying I passed 2 parameters?
Yoriz write Dec-10-2021, 02:44 PM:
Please post all code, output and errors (in their entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Reply
#2
This
result = agent.agentprogram(agent_percept)
calls
Agent.agentprogram(agent, agent_percept)
2 arguments.

An easy way to figure out these kinds of problems is adding a print.
    def agentprogram(percept:tuple, *args, **kvargs):
        print('agentprogram arguments', percept, args, kvargs)
When I run this it prints
Output:
agentprogram arguments <__main__.Agent object at 0x000002C18ADB8400> (((('A', 'Clean'), ('B', 'Dirty')), 'B'),) {}
2 arguments.
There are three ways to solve this problem. Correct agentprogram() to have "self" as the first argument, correct agentprogram() to have cls as the first argument, get rid of the Agent class.

This works and it is easy
    def agentprogram(self, percept:tuple):
But I don't think it is a good solution. Agent is not a class. Maybe the problem is that you snipped away all the important parts of Agent to make a short example for your post. If so, then use the suggested solution above. If not, look at the Agent class and tell me what it is. The __init__() creates two instance variables, but neither of these are used. The agentprogram() method doesn't use any instance variables. This could be turned into a class method (use @classmethod decorator), but it doesn't use any class variables either. Agent is a useless class wrapper around the agentprogram() function.
def agentprogram(percept:tuple):
    table = {((('A','Clean'),('B','Clean')),'A'):'NoOp',
    ((('A','Clean'),('B','Clean')),'B'):'NoOp',
    ((('A','Clean'),('B','Dirty')),'A'):'Right',
    ((('A','Clean'),('B','Dirty')),'B'):'Suck',
    ((('A','Dirty'),('B','Clean')),'A'):'Suck',
    ((('A','Dirty'),('B','Clean')),'B'):'Left',
    ((('A','Dirty'),('B','Dirty')),'A'):'Suck',
    ((('A','Dirty'),('B','Dirty')),'B'):'Suck',
    }

    action = table[percept]
    return action

#creating the tuple I will pass
agent_percept = ((('A','Clean'),('B','Dirty')),'B')
result = agentprogram(agent_percept)
print(result)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Puzzling import issue that I have no idea how to solvr starseeker 3 524 Feb-21-2024, 05:07 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

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