python - 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
解决方案
for loop in range(4) instead of range[4]
因为它从 0 迭代到 range-1
推荐阅读
- php - 如何在 Windows 7 的 XAMPP 上运行的 PHP 应用程序中启用 SSL?
- javascript - 对基于另一个数组的数组进行排序,并用 0 或空字符串 javascript 替换不匹配的数据
- dart - 飞镖:什么
在函数名之后 - tomcat - 如何在 Jakarta / Java EE 中连续运行一个进程(没有网站请求)?
- haskell - 如何在haskell中仅导入特定实例
- azure-devops - 从 REST API 传递到构建管道 yml 的变量未正确获取
- plot - 带有gnuplot的奇怪图
- amazon-web-services - 有没有办法直接从我的 React 前端调用 API Gateway
- asp.net - 缺少嵌套在 FindControl 中的东西,但总是得到 Null
- javascript - 有没有办法从 typecript 使用 npm javascript 模块?