Error in Task2.py

Hey all ,
So i have this issue with Task2.py for the prerequisites exam

my robot stop within 1 meter in the wall and turn right 90
but I’m facing this issue

s = “Turned robot " + clockwise + " for " + str(time) + " seconds”
TypeError: cannot concatenate ‘str’ and ‘int’ objects

here is my code :

from robot_control_class import RobotControl
import time

# Create an instance of the RobotControl class
robotcontrol = RobotControl()

# Keep moving the robot forwards until it detects an obstacle at less than 1 meter in front of it
while True:
    a = robotcontrol.get_laser(360)
    print("Current distance to wall: %f" % a)
    if a <= 1.3:
        robotcontrol.stop_robot()
        print("Current distance to wall: %f" % a)
        break
    robotcontrol.move_straight()
    time.sleep(0.5)  # add a delay to limit the frequency of the loop

# Turn the robot 90 degrees to the right
robotcontrol.turn(90, -1, 1.8)

# Concatenate a string and an integer
clockwise = "clockwise"
time = 5
s = "Turned robot " + clockwise + " for " + str(time) + " seconds"

# Print the resulting string
print(s)

Hi @nass2usa ,

That is because you have redeclared time variable as 5.
You have already declared time variable when you did import time - so you cannot use time as a variable anymore. Change the line time = 5 to something like t = 5.
Then print the statement:

# Concatenate a string and an integer
clockwise = "clockwise"
t = 5
s = "Turned robot " + clockwise + " for " + str(t) + " seconds"

This time it should work. Your code has no other errors otherwise.

Let me know if this does not work.

Regards,
Girish

2 Likes