【问题标题】:"TypeError: 'tuple' object is not callable" error in Python while trying to get a object in tuple by indexing尝试通过索引获取元组中的对象时,Python 中出现“TypeError:‘元组’对象不可调用”错误
【发布时间】:2021-07-25 04:07:41
【问题描述】:

每当我尝试获取元组中的对象时,都会收到此错误“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=Word_List[m]

标签: python indexing tuples typeerror


【解决方案1】:

Word_List(m)放括号表示python这是一个函数。但是,您已将其定义为元组。因此你得到了错误。

你必须使用方括号

word=Word_List[m]

【讨论】:

    猜你喜欢
    • 2015-03-11
    • 2023-03-27
    • 2017-11-17
    • 2021-09-30
    • 1970-01-01
    • 2023-03-07
    • 1970-01-01
    • 2020-08-29
    • 1970-01-01
    相关资源
    最近更新 更多