Click here to Skip to main content
15,881,803 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
This is the output I get print(findSlowestUnit(200)) File "C:/Users/bhavika/Desktop/shonal/shonal/Demo level 3 script and ppts/Python/games/Basic_game/debug_code.py", line 8, in findSlowestUnit for currentUnit in units: TypeError: 'int' object is not iterable

What I have tried:

Python
MAX_NUMBER = 2 ** 32 - 1
# Finds the unit with minimum speed.
# If there are more than 1 units then the first occured
# For an empty list the function returns None
def findSlowestUnit(units):
    slowestUnit = None;
    minSpeed = 0
    for currentUnit in units:
       if currentUnit['speed'] > minSpeed:
       slowestUnit = currentUnit
       minSpeed = currentUnit.speed
    return currentUnit
Posted
Updated 13-May-21 16:27pm
v2

==>> TypeError: 'int' object is not iterable –

You are attempting for loop over the units object.
Python
for currentUnit in units

Most likely the units object is not a collection and so it is failing.
 
Share this answer
 
Another problem in your code
Python
MAX_NUMBER = 2 ** 32 - 1
# Finds the unit with minimum speed.
# If there are more than 1 units then the first occured
# For an empty list the function returns None
def findSlowestUnit(units):
    slowestUnit = None;
    minSpeed = 0
    for currentUnit in units:
       if currentUnit['speed'] > minSpeed:
           slowestUnit = currentUnit # indentation was wrong here
           minSpeed = currentUnit.speed # indentation was wrong here
    return currentUnit

With Python, indentation is very important, it is the structure of your code.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900