如何消除"' str'对象不可调用"?

时间:2015-12-12 05:30:33

标签: python python-3.x

我在下面的代码中收到以下错误,并且不知道发生了什么。我非常喜欢初学者,需要一些帮助。我试图在控制台中制作一个非常基本的井字游戏,但我遇到了麻烦。

Traceback (most recent call last):
File "D:/Eric/Projects/Tic Tac Toe/Tic Tac Toe.py", line 68, in <module>
printboard()
File "D:/Eric/Projects/Tic Tac Toe/Tic Tac Toe.py", line 15, in printboard
print("",c[0],"|",c[1],"|",c[2],"\n-----------\n",c[3],"|",c[4],"|",c[5],"\n-----------\n",c[6],"|",c[7],"|",c[8])
TypeError: 'str' object is not callable

代码:

import random, time

#Variables
c = [str(i) for i in range(1,10)] #Cells
cpuletter = "string input"
coinoptions = ["heads", "tails"]
coinflip = random.choice(coinoptions)
play = ""
playercell = 0

#Functions
def printboard():    #Prints the gameboard with the cell variables in the spaces
   print("",c[0],"|",c[1],"|",c[2],"\n-----------\n",c[3],"|",c[4],"|",c[5],"\n-----------\n",c[6],"|",c[7],"|",c[8])
   return

#Introduction
print("Welcome to Tic Tac Toe. Below is the playing table.") #Welcome + Explanation
printboard()
print("The playing spaces are numbered 1 through 9.\nYou will use these numbers to refer to the places you would like to play your piece.\n")

#X or O?
playerletter = input("Would you like to be X's or O's?\n")
playerletter = playerletter.capitalize()
playerletter = playerletter[0]

while playerletter not in ["X", "O"]:     #Confirm choice == "X" or "O." If not, ask again.
   playerletter = input("""Sorry, that's not a valid option. Please choose "X" or "O."\n""")
   playerletter = playerletter.capitalize()
   playerletter = playerletter[0]

if playerletter == "X":
    cpuletter = "O"

elif playerletter == "O":
    cpuletter = "X"


#Who goes first?
playercoin = input("Randomizing who goes first:\nDo you choose heads or tails?\n")      #Asking for "heads" or "tails"
playercoin = playercoin.lower()

while playercoin not in ["heads", "tails"]:     #Confirm choice == "Heads" or "Tails." If not, ask again.
   playercoin = input("""Sorry, that's not a valid option. Please choose "heads" or "tails."\n""")
   playercoin = playercoin.lower()

print("...")
time.sleep(1)       #Waits 1 seconds

if coinflip != playercoin:      #Tells player who goes first
    print("It landed " + coinflip + "! I will go first.")
    play = 0

elif coinflip == playercoin:
    print("It landed " + coinflip + "! You will go first.")
    play = 1



#Game
input("""Ready to play? Press "Enter" to begin!\n""")

if play == 0:
    random_index = random.randrange(9) #Randomly selects a cell to change to cpuletter
    c[random_index] = cpuletter
    print = ("Here is my move.")
    printboard()

elif play == 1: #Allows player to choose cell to change to playerletter
    printboard()
    playercell = int(input("It's your turn. Type a cell number to choose where you play.\n"))
    playercell = playercell - 1
    c[playercell] = playerletter
    printboard()

编辑:忘了说这只发生在计算机先行时,而不是播放器。我修复了print =(&#34;这是我的举动。&#34;)但我仍然遇到问题。

3 个答案:

答案 0 :(得分:4)

Secedit.exe /export /cfg c:\cfg.txt

该行将内置print = "Here is my move." 函数重新分配给字符串。不要那样做。

答案 1 :(得分:2)

为什么要制作如此复杂的打印功能?格式化现在非常方便:

move = ("Here is my move")

最后修复你的错误,改变第65行:

let url = NSBundle.mainBundle().URLForResource("index", withExtension:"html", subdirectory: "www")
let request = NSURLRequest(URL: url!)
webView.loadRequest(request)

答案 2 :(得分:0)

我运行了你的代码,但我没有收到任何错误。这个特殊的 TypeError 通常是由程序员错误地用括号切换方括号引起的(因此“调用”它就好像字符串是一个函数,它不是。)。请再次检查并运行您的代码。

编辑:解决方案:将第65行的print = "Here is my move."变量名更改为其他内容,例如movePrint = "Here is my move"