Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialMárk Mészáros
2,966 PointsI get 'TypeError: 'float' object is not iterable, could someone help me please?
Fellow learners!
I need some help with this piece of code.
I get the following error log when running the file:
Traceback (most recent call last):
File "dungeon.py", line 54, in <module>
valid_moves = get_moves(player)
File "dungeon.py", line 38, in get_moves
x, y = player
TypeError: 'float' object is not iterable
I understand that there is something going wrong at line 38, with x and y, but I am nut sure how to tackle the float problem. I thought that player is a tuple, and x, y are integers unpacked from this tuple, so no clue about where float comes in the picture. Any explanation would be highly appreciated! The full code is below:
import os
import random
CELLS =[(0,0),(1,0),(2.0),(3.0),(4.0),
(0,1),(1,1),(2.1),(3.1),(4.1),
(0,2),(1,2),(2.2),(3.2),(4.2),
(0,3),(1,3),(2.3),(3.3),(4.3),
(0,4),(1,4),(2.4),(3.4),(4.4)]
def clear_screen():
os.system("cls" if os.name =="nt" else "clear")
def get_positions():
return random.sample(CELLS,3)
return player, door, monster
player, door, monster = get_positions()
def move_player(player, move):
x, y = player
if move == "LEFT":
x -= 1
if move == "RIGHT":
x += 1
if move == "UP":
y -= 1
if move == "DOWN":
y += 1
return x, y
def get_moves(player):
moves = ["LEFT","RIGHT","UP","DOWN"]
x, y = player
if x == 0:
moves.remove("LEFT")
if x == 4:
moves.remove("RIGHT")
if y == 0:
moves.remove("UP")
if y == 4:
moves.remove("DOWN")
return moves
while True:
valid_moves = get_moves(player)
clear_screen()
print("Welcome to the Dungeon!")
print("You are now in the {} room".format(player))
print("You can move to {}".format(", ".join(valid_moves)))
print("To quit type QUIT")
move = input("> ")
move = move.upper()
if move == "QUIT":
break
if moves in valid_moves:
player = move_player(player, move)
else:
print("\n You hit a wall!")
continue
2 Answers
Mike Wagner
23,559 PointsMany of your cells were defined with x.y
(period) when they should be x,y
(comma). If you fix those cells, you shouldn't have the "float" problem.
Edit as a side note, you have a second and unreachable return statement in your get_positions() method. You'll have to fix that in order to get the expected behavior.
Márk Mészáros
2,966 PointsThanks for the help and for pointing out the return trouble. Much appreciated!
Márk Mészáros
2,966 PointsQuestion is cleared, it is much more easy to read it now ;) Can it be that some of the the CELLS list's tuples are actually not tuples because they have a . (dot) and not a , (comma)? Have to check that out...
Márk Mészáros
2,966 PointsCorrecting the CELLS sorted out the float problem part :) Sometimes just asking the question and having an overview helps in solving the problem ;) There is still a bug, at the end, in the if statement, there should be 'move' instead of 'moves' Thanks for the support Mike Wagner! ;)
Mike Wagner
23,559 PointsMárk Mészáros - Ah, yes. I see the 'moves'. I didn't read down that far, haha. No problem. Glad to help.
Mike Wagner
23,559 PointsMárk Mészáros - I am still sort of curious what you were attempting to do with your double return statement in get_positions
. For some reason I can't wrap my head around it, haha.
Mike Wagner
23,559 PointsMike Wagner
23,559 PointsThe formatting on your question makes it difficult to discern the structure of your code. If you could edit your question to use the backtick ` character (3 times on the line above and below your code) it will be easier to help you sort out.