Error!!! cant concatenate the tuple to non float

2024/11/24 10:38:44
stack = []closed = []currNode = problem.getStartState()stack.append(currNode)while (len(stack) != 0):node = stack.pop()if problem.isGoalState(node):print "true"closed.append(node)else:child = problem.getSuccessors(node)if not child == 0:stack.append(child)closed.apped(node)return None

code of successor is:

def getSuccessors(self, state):"""Returns successor states, the actions they require, and a cost of 1.As noted in search.py:For a given state, this should return a list of triples, (successor, action, stepCost), where 'successor' is a successor to the current state, 'action' is the actionrequired to get there, and 'stepCost' is the incremental cost of expanding to that successor"""successors = []for action in [Directions.NORTH, Directions.SOUTH, Directions.EAST, Directions.WEST]:x,y = statedx, dy = Actions.directionToVector(action)nextx, nexty = int(x + dx), int(y + dy)if not self.walls[nextx][nexty]:nextState = (nextx, nexty)cost = self.costFn(nextState)successors.append( ( nextState, action, cost) )# Bookkeeping for display purposesself._expanded += 1 if state not in self._visited:self._visited[state] = Trueself._visitedlist.append(state)return successors

The error is:

File line 87, in depthFirstSearchchild = problem.getSuccessors(node)File  line 181, in getSuccessorsnextx, nexty = int(x + dx), int(y + dy)
TypeError: can only concatenate tuple (not "float") to tuple

When we run the following commands:

 print "Start:", problem.getStartState()print "Is the start a goal?", problem.isGoalState(problem.getStartState())print "Start's successors:", problem.getSuccessors(problem.getStartState()) 

we get:

Start: (5, 5)
Is the start a goal? False
Start's successors: [((5, 4), 'South', 1), ((4, 5), 'West', 1)]
Answer

Change this:

nextx, nexty = int(x + dx), int(y + dy)

to this:

print x, y, dx, dy, state
nextx, nexty = int(x + dx), int(y + dy)

I guarantee you will see () around something besides state. That means your value is a tuple:

int(x + dx), int(y + dy)

You cannot concatenate a float and a tuple and convert the result to integer, it just won't work:

In [57]: (5, 5) + 3.0
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)c:\<ipython console> in <module>()TypeError: can only concatenate tuple (not "float") to tuple
https://en.xdnf.cn/q/120539.html

Related Q&A

Using R to fit data from a csv with a gamma function?

Using the following data: time_stamp,secs,count 2013-04-30 23:58:55,1367366335,32 2013-04-30 23:58:56,1367366336,281 2013-04-30 23:58:57,1367366337,664 2013-04-30 23:58:58,1367366338,1255 2013-04-30…

regex multiple string match in a python list

I have a list of strings like this:["ra", "dec", "ra-error", "dec-error", "glat", "glon", "flux", "l", "b"]I ne…

python IndentationError: expected an indented block [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.This question was caused by a typo or a problem that can no longer be reproduced. While similar q…

how to tell an infinite loop to end once one number repeats twice in a row (in python 3.4)

The title says it all. I have an infinite loop of randomly generated numbers from one to six that I need to end when 6 occurs twice in a row.

Is it possible to customize the random function to avoid too much repetition of words? [duplicate]

This question already exists:Customizing the random function without using append, or list, or other container?Closed last year.Theoretically, when randomization is used, the same word may be printed …

country convert to continent

def country_to_continent(country_name):country_alpha2 = pc.country_name_to_country_alpha2(country_name)country_continent_code = pc.country_alpha2_to_continent_code(country_alpha2)country_continent_name…

Iterate through a list and delete certain elements

Im working on an assignment in my computer class, and Im having a hard time with one section of the code. I will post the assignment guidelines (the bolded part is the code Im having issues with):You a…

How to count numbers in a list via certain rules?

Just to say I have a str and a list of strs and I want to count how many strs in the list that is contained in a str. Is there an elegant way to do that?For example, l = {"foo", "bar&qu…

Count character occurrences in a Python string

I want to get the each character count in a given sentence. I tried the below code and I got the count of every character but it displays the repeated characters count in the output. How to delete repe…

The Concept Behind itertoolss product Function

so basically i want to understand the concept of product() function in itertools. i mean what is the different between yield and return. And can this code be shorten down anyway.def product1(*args, **k…