python - 尝试通过索引获取元组中的对象时,Python 中出现“TypeError:‘元组’对象不可调用”错误
问题描述
每当我尝试获取元组中的对象时,都会收到此错误“TypeError:'tuple' object is not callable”。我的代码是 -
import random
import sys
inp = input("Press ENTER to start.")
sys.stdout.write("Rules --\nIn this game, you will have to correctly guess the word. If you can do that successfully, YOU WIN!\n")
sys.stdout.write("You will given (number of letters of the word + 2) chance. In each chance you will have to guess a letter of the word\n")
sys.stdout.write("If you run out of chance, you lose. :(\n")
while True:
inp = input("Ready(Y/N)? ")
if 'Y' in inp or 'y' in inp:
print("Okay")
break
elif 'n' in inp or 'N' in inp:
sys.exit
Word_List = ('banana', 'cat', 'mat', 'apple', 'pineapple', 'mango')
m = random.randint(1, 6)
word = Word_List(m)
chances = len(word) + 2
for x in range(len(word)):
sys.stdout.write('_ ')
任何人都可以发现错误吗?
解决方案
Word_List(m)
放括号表示python这是一个函数。但是,您已将其定义为元组。因此你得到错误。
你必须使用方括号
word=Word_List[m]
推荐阅读
- postgresql - PostgreSQL 水平排列计算列
- angular - Nativescript Angular - RadListView 重新排序奇怪的 IOS 动画
- dynamics-crm - Dynamics CRM - 使用客户端扩展(站点地图)导入解决方案不会删除条目
- indexing - DynamoDB 中没有分区键的查询
- postgresql - Postgresql 将数据从 jsonb 数组迁移到 jsonb
- python - AttributeError:使用 NetworkX 中的 greedy_modularity_communities 时,“NoneType”对象没有属性“get”
- python - 绘图时多处理不起作用
- corrily - 如何使用 Corrily 的创建订阅/收费端点处理折扣
- python - Python:从另一个文件动态访问变量
- javascript - 所有文档均未更新猫鼬