首页 > 解决方案 > 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

标签: pythonpyscripter

解决方案


for loop in range(4) instead of range[4]

因为它从 0 迭代到 range-1


推荐阅读