【问题标题】:TypeError: 'str' object is not callable python 3.2.5TypeError:'str'对象不可调用python 3.2.5
【发布时间】:2017-02-28 19:12:37
【问题描述】:

我的代码有什么问题?我不断收到错误 TypeError: 'str' object is not callable。我不明白为什么我一直收到这个错误,当一切都被定义并在需要时被强制到 str 时,如果不是超过需要的话。

import time

hallNumber = ""

if hallNumber == str(1):
    print('You find a window')
else :
    print('You find what you think is a door')

def dispIntro():
        text_file = open("E:\Intro_game_develop\projects\project_one\intro.txt", 'r')
        whole_thing = text_file.read()
        print(whole_thing)
        text_file.close()

def chooseHall():
    hall = ''
    while hall != '1' and hall != '2':
            print('Which hall do you choose to go down? 1, or 2')
            hall = input()

    return hall


def checkHall(chosenHall):
        print('You walk down the hall...')
        time.sleep(1)
        print('You walk towards the light...')
        time.sleep(1)

        print('You reach out for it and...')
        time.sleep(1)
##hallNumber = ""
##
##if hallNumber == str(1):
##    print('You find a window')
##else :
##    print('You find what you think is a door')


def chosenPath(hallNumber):
    print("I'm in chosenPath")
    path = str(hallNumber())
    if path == str(hallNumber(1)):
            text_file = open("E:\Intro_game_develop\projects\project_one\window.txt", 'r')

            whole_thing = text_file.read()

            print(whole_thing)

            text_file.close()

    else:
             text_file = open("E:\Intro_game_develop\projects\project_one\door.txt",'r')

             whole_thing = text_file.read()

             print(whole_thing)

             text_file.close()

             return path

【问题讨论】:

  • hallNumber(1) 应该做什么?
  • 或者甚至只是上面一行的hallNumber(),就此而言......
  • 你将hallNumber设置为一个字符串,然后你称之为hallNumber(),但它不是一个函数,不能像函数一样调用一个字符串
  • 提供运行脚本时运行的实际代码..

标签: python typeerror


【解决方案1】:

尝试运行这个:

hallNumber = "foo"
path = str(hallNumber())

在第 1 行,hallNumber 设置为字符串。

在第 2 行,hallNumber 是一个字符串,它像带有 hallNumber() 的函数一样被调用,然后该函数的输出被转换为带有 str() 的字符串。但hallNumber 不是可调用函数——它是一个字符串。

因此TypeError: 'str' object is not callable

【讨论】:

    猜你喜欢
    • 2020-09-16
    • 1970-01-01
    • 1970-01-01
    • 2013-08-05
    • 2012-06-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-07
    相关资源
    最近更新 更多