pythonpyscripter

How do I resolve a type error in this code?


def turn_clockwise(point):
    all_point = ["N", "E", "S", "W"]
    for loop in range[4]:
        if all_point[loop] == point:
            if loop == 3:
                return "N"
            else:
                return all_point[loop + 1]

Message from the Python interpreter on PyScripter:

Traceback (most recent call last):
File "D:\Documents\Pyscripter practice\Chp.6 Exercises - Fruitful functions.py", line 25, in test_suite()
File "D:\Documents\Pyscripter practice\Chp.6 Exercises - Fruitful functions.py", line 21, in test_suite test(turn_clockwise("N") == "E")
File "D:\Documents\Pyscripter practice\Chp.6 Exercises - Fruitful functions.py", line 5, in turn_clockwise for iteration in range[4]:
TypeError: 'type' object is not subscriptable


Solution

  • you need to use () brackets instead of [] because range is a Class which we need to call.

    
    
    def turn_clockwise(point):
        all_point = ["N", "E", "S", "W"]
        for loop in range(4):
            if all_point[loop] == point:
                if loop == 3:
                    return "N"
                else:
                    return all_point[loop + 1]