python TypeError“ +不支持的操作数类型:'int'和'str'”为什么我得到这个?

时间:2018-09-29 16:35:25

标签: python

当我运行这段代码时,它会问我您想选择什么动作:正如它的意图一样。但是,如果我进入岩石,它会说

popularity = str(int(open("/Users/timothy/Documents/" + move[playermove + ".txt",r]).read()) + 1)
TypeError: unsupported operand type(s) for +: 'int' and 'str'.

我检查了所有内容都是字符串,为什么会这样?

我的代码:

while True:
    move = ["rock", "paper", "scissors"]
    movenumber = 0
    computermove = ChooseMove()
    go = 1
    while go == 1:
        playermove = move.index(input("What move do you want to choose:   "))
        popularity = str(int(open("/Users/timothy/Documents/" + move[playermove + ".txt",r]).read()) + 1)
        (open("/Users/timothy/Documents/" + move[playermove] + ".txt","w")).write(popularity)
        go = 2
    print ()
    print ("The computer picked " + move[computermove])
    print ()
    if playermove - computermove == 1:
        print ("Player won")
    elif playermove - computermove == 2:
        print ("Player lost")
    elif playermove - computermove == -1:
        print ("Player lost")
    elif playermove - computermove == -2:
        print ("Player won")
    else:
        print ("It's a draw")

1 个答案:

答案 0 :(得分:1)

您在编写时出错

np.savetxt("file", [['r1c1', 'r1c2'], ['r2c1', 'r2c2']], delimiter=';', fmt='%s')

应该在此位置将move[playermove + ".txt",r]) 替换为r

'r'