Please tell me why this code:
works when I run the code on my own PC, but …
I get a TypeError in the Codecademy environment.
Context: I copied the files (robot_race.py, robot_race_functions.py and maze_data_1.csv) into a folder on my PC. (I did this to take advantage of Visual Studio Code’s editing and validation).
I completed steps 1 to 13 and got a result (after relying heavily on Codecademy hints!):
----- RESULTS -----
When I ran the same code in the Codecademy environment, however, I got this error message:
Traceback (most recent call last):
File “robot_race.py”, line 75, in
rr.print_results(bot_scores)
File “/home/ccuser/workspace/the-great-robot-race-python-project-workspace/robot_race_functions.py”, line 41, in print_results
bot_score_data.sort(key=lambda b: b.score)
TypeError: ‘<’ not supported between instances of ‘Counter’ and ‘Counter’
Traceback (most recent call last):
File “robot_race.py”, line 75, in
rr.print_results(bot_scores)
File “/home/ccuser/workspace/the-great-robot-race-python-project-workspace/robot_race_functions.py”, line 41, in print_results
bot_score_data.sort(key=lambda b: b.score)
TypeError: ‘<’ not supported between instances of ‘Counter’ and ‘Counter’
Here is my code in robot_race.py. It works using Visual Studio Code accessing files copied onto my own laptop; but throws a runtime error in the Codecademy environment.
import robot_race_functions as rr
from collections import deque, Counter, namedtuple
from time import time, sleep
maze_file_name = 'C:\Temp\maze_data_1.csv' # needs full path - don't understand why
seconds_between_turns = 0.3
max_turns = 35
# Initialize the robot race
maze_data = rr.read_maze(maze_file_name)
# rr.print_maze(maze_data) # had to add extra line to read_maze function to get it to recognise maze_file_name on my PC
walls, goal, bots = rr.process_maze_init(maze_data)
# Populate a deque of all robot commands for the provided maze
robot_moves = deque()
num_of_turns = 0
while not rr.is_race_over(bots) and num_of_turns < max_turns:
# For every bot in the list of bots, if the bot has not reached the end, add a new move to the robot_moves deque
# Add your code below!
# Checkpoint 5
for bot in bots:
robot_moves.append(rr.compute_robot_logic(walls, goal, bot))
num_of_turns += 1
# Count the number of moves based on the robot names
# Add your code below!
# Checkpoint 6
move_count = Counter(move[0] for move in robot_moves)
# Count the number of collisions by robot name
# Add your code below!
# Checkpoint 7
collision_count = Counter(move[0] for move in robot_moves if move[2] == True)
# Create a namedtuple to keep track of our robots' points
# Add your code below!
# Checkpoint 8
BotScoreData = namedtuple('BotScoreData',['name', 'num_moves', 'num_collisions', 'score'])
# Calculate the scores (moves + collisions) for each robot and append it to bot_scores
bot_scores = []
# Add your code below!
# Checkpoint 9
for bot in bots:
bot_scores.append(BotScoreData(bot.name, move_count, collision_count, move_count + collision_count))
# Populate a dict to keep track of the robot movements
bot_data = {}
# Add your code below!
# Checkpont 10
for bot in bots:
bot_data[bot.name] = bot
# Move the robots and update the map based on the moves deque
while len(robot_moves) > 0:
# Make sure to pop moves from the front of the deque
# Add your code below!
# Checkpoint 11
bot_name, direction, has_collided = robot_moves.popleft()
bot_data[bot_name].process_move(direction)
# Update the maze characters based on the robot positions and print it to the console
rr.update_maze_characters(maze_data, bots)
rr.print_maze(maze_data)
sleep(seconds_between_turns - time() % seconds_between_turns)
# Print out the results!
# Checkpoint 12
rr.print_results(bot_scores)