TypeError:'str' 对象在 main.py 的第 200 行不可调用

Posted

技术标签:

【中文标题】TypeError:\'str\' 对象在 main.py 的第 200 行不可调用【英文标题】:TypeError: 'str' object is not callable on line 200 in main.pyTypeError:'str' 对象在 main.py 的第 200 行不可调用 【发布时间】:2017-10-15 14:40:07 【问题描述】:

我不知道它在说什么字符串,所以我有点卡住了。也不知道字符串是如何不可调用的,它们是指变量还是什么?

from time import *

sleep(1)
axecount = 0

#intro

def fail():
  global rcommand
  rcommand = None
  sleep(1)
  print('---------------------------------------------')
  print('What? I did not understand, lol. Please time travel back to the past and retry.')
  sleep(1)
  print('')
  print('| RETURN |')
  print('---------------------------------------------')
  rcommand = input('Command:')
  if rcommand == 'RETURN':
    play()
  else:
    fail()
  print('---------------------------------------------')

def play():
  global axecount
  global rcommand
  rcommand = None
  print('---------------------------------------------')
  print('Welcome to...')
  print('')
  sleep(1)
  print('-=-=An Adventure through Time=-=-')
  print('')
  sleep(1)
  print('| PLAY | HELP | STORY |')
  print('')
  print('---------------------------------------------')
  sleep(1)
  play = (input('Command:'))
  print('---------------------------------------------')

  #if player chooses to play

  if play == 'PLAY':
    sleep(1)
    print('Well, let us begin!')
    print('')
    sleep(1)

    #map selection begins

    print('Select a map: DIMENSION, LANDMASS')
    print('---------------------------------------------')
    mapchooser = (input('Map chosen:'))

    #if player chooses the dimension map then...

    if mapchooser == 'DIMENSION':
      print('---------------------------------------------')
      print('Map DIMENSION loading...')
      print('---------------------------------------------')
      sleep(2)
      print('Loaded!')
      print('---------------------------------------------')
      sleep(1)
      print('-Stage 1-')
      print('---------------------------------------------')
      sleep(1)
      print('There are 3 doors. One at your LEFT, RIGHT, and FORWARD. Where do you go?')
      sleep(1)
      print('One of them has a monster... Good luck.')
      print('---------------------------------------------')

      #direction that he chooses to go at the first door choice

      def firstdoor():
        global axecount
        firstdoor = input('Command:')
        print('---------------------------------------------')

        #if player chooses to go left

        if firstdoor == 'LEFT':

          #dies

          sleep(1)
          print('Ouch! A monster! Travel back in time and retry!')
          print('---------------------------------------------')
        elif firstdoor == 'RIGHT':
          sleep(1)
          print('Wow, a chest has been found!')
          sleep(1)
          print('You get an axe!')
          print('Type INVENTORY as your command to view your items.')
          axecount += 1
          print('---------------------------------------------')

        elif firstdoor == 'INVENTORY':

          #opens inventory of player

          sleep(1)
          print('You have... ')
          print('Axes - ' + axecount)
          firstdoor()

      firstdoor()


    #if player chooses the landmass map then...

    elif mapchooser == 'LANDMASS':
      print('---------------------------------------------')
      print('Map LANDMASS loading...')
      sleep(3)
      print('Map LANDMASS not yet created. Coming soon!')
      print('Please time travel back to the past and retry.')
      sleep(1)
      print('| RETURN |')
      print('---------------------------------------------')
      rcommand = input('Command:')
      if rcommand == 'RETURN':
        play()
      else:
        fail()
      print('---------------------------------------------')

    #if player enters some nonsense that isnt correct

    else:
      sleep(1)
      print('---------------------------------------------')
      print('What? I did not understand, lol. Please time travel back to the past and retry.')
      sleep(1)
      print('')
      sleep(1)
      print('Look at HELP for more information.')
      print('---------------------------------------------')
      sleep(1)
      print('| RETURN |')
      print('---------------------------------------------')
      rcommand = input('Command:')
      if rcommand == 'RETURN':
        play()
      else:
        fail()
      print('---------------------------------------------')

  #help screenH

  elif play == 'HELP':
    sleep(1)
    print('Answer the command exactly how it looks like on the selection.')
    print('Example: Enter PLAY if it says PLAY')
    print('')
    sleep(1)
    print('There are monsters in some rooms and chests in some rooms.')
    print('Monsters kill you and the game ends.')
    print('Chests give you materials to try and kill the monster.')
    print('')
    sleep(1)
    print('Restart the game to go back to the selection screen.')
    sleep(1)
    print('---------------------------------------------')
    print('| RETURN |')
    print('---------------------------------------------')
    rcommand = input('Command:')
    if rcommand == 'RETURN':
      play()
    else:
      fail()


  elif play == 'STORY':
    sleep(1)
    print('You enter a dungeon but not all is what it seems...')
    sleep(1)
    print('The dungeon is haunted by powerful magic...')
    sleep(1)
    print('And each time you go to a room, the other rooms age by 10 years.')
    sleep(1)
    print('Is luck on your side?')
    sleep(1)
    print('---------------------------------------------')
    print('| RETURN |')
    print('---------------------------------------------')
    rcommand = input('Command:')
    if rcommand == 'RETURN':
      play()
    else:
      fail()
    print('---------------------------------------------')

  #if player enters some nonsense that isnt correct

  else:
    fail()

play()

【问题讨论】:

这是堆栈跟踪有用的地方。 抱歉我的无知,但那是什么? 堆栈跟踪可以告诉您错误是什么以及它在哪一行。 但无论如何,您有一个名为play 的变量,并且您还试图调用一个名为play 的函数。给这两个东西起不同的名字。 @MapperificMappingandGaming - 您应该将 khelwood 的答案标记为正确 【参考方案1】:

你有一个名为play的变量在线,就在这里:

  sleep(1)
  play = (input('Command:'))
  print('---------------------------------------------')

此命名会干扰您的函数命名,将此变量名称更改为尚未用于函数的名称应该可以解决问题。

【讨论】:

【参考方案2】:

你有一个名为play的变量

play = (input('Command:'))

现在play 是一个字符串,所以如果你尝试调用play(),它会尝试调用一个字符串,就好像它是一个函数一样。

如果您将 play 变量称为其他名称,它不会与函数名称 play 冲突。

【讨论】:

以上是关于TypeError:'str' 对象在 main.py 的第 200 行不可调用的主要内容,如果未能解决你的问题,请参考以下文章

Django 预览,TypeError:'str' 对象不可调用

TypeError:需要一个类似字节的对象,而不是'str'-python 2到3 [重复]

TypeError:“str”对象的描述符“encode”不适用于“tuple”对象

TypeError:无法将'int'对象隐式转换为str(python)

TypeError:在尝试发送 http 请求时需要一个类似字节的对象,而不是“str”

TypeError:连接 csv 文件时,“str”对象不是迭代器