python-3.x - AttributeError:“列表”对象没有属性“查找”
问题描述
以下代码导致 AttributeError: 'list' object has no attribute 'find' 我不知道如何修复它:
import string
letters_list = list(string.ascii_lowercase)
messages = input()
current_loc = -1
times = 0
if messages != 'halt':
for char in messages:
loc = letters_list.find(char)
if loc//3 == current_loc//3 or (loc//3 > 7 and current_loc//3 > 7):
times += 2
if loc % 3 == 0:
times += 1
elif loc % 3 == 1:
times += 2
else:
times += 3
current_loc = loc
print(times)
解决方案
不确定您对输出的期望。但不是find
,而是尝试使用index
来获取字符的索引。
import string
letters_list = list(string.ascii_lowercase)
messages = input()
current_loc = -1
times = 0
if messages != 'halt':
for char in messages:
loc = letters_list.index(char)
if loc // 3 == current_loc // 3 or (loc // 3 > 7 and current_loc // 3 > 7):
times += 2
if loc % 3 == 0:
times += 1
elif loc % 3 == 1:
times += 2
else:
times += 3
current_loc = loc
print(times)
推荐阅读
- dialogflow-es - 在 Google Actions 的情况下,Transaction API 不起作用
- spring-mvc - 在对休息控制器执行测试时在 spock 测试用例中出错
- javascript - Redux observable 不处理后端响应
- oracle - 除了状态行,不显示 Oracle Forms 消息
- spring-amqp - ImmediateRequeueAmqpException 似乎不起作用
- c# - 如何使用声纳扫描仪扫描 C# 项目
- node.js - Button Component中的MUI奇怪代码,希望有人能解释一下
- f# - 4.5 版中的阴影
- python - 关于通过 ssh 将 opencv 库安装到树莓派 4 的问题
- php - CakePHP:Bin 蛋糕烘焙表不存在