TypeError:' str'对象不可调用python 3.2.5

时间:2017-02-28 19:12:38

标签: python typeerror

我的代码出了什么问题?我一直收到错误TypeError:' str'对象不可调用。我不明白为什么我继续得到那个错误,当一切都被定义并且在需要时被强制为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

1 个答案:

答案 0 :(得分:0)

尝试运行此:

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

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

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

因此TypeError: 'str' object is not callable