繁体   English   中英

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

[英]python TypeError "unsupported operand type(s) for +: 'int' and 'str'" Why am I getting this?

当我运行这段代码时,它会问我你想选择什么动作:正如它的意思。 但如果我进入岩石和其他任何东西它会说

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")

你写错了

move[playermove + ".txt",r])

它应该是您还应该将r替换为'r'

move[playermove] + ".txt",'r')

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM