Timothy Harris
Timothy Harris

Reputation: 33

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

When I run this code I, it will ask me What move do you want to choose: just as it's meant to. But if I enter rock and anything else it will say

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

I have checked that everything is string so why is this the case?

My code:

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

Upvotes: 1

Views: 69

Answers (1)

Sheldore
Sheldore

Reputation: 39072

You had an error in writing

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

It should be where you should also replace r by 'r'

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

Upvotes: 1

Related Questions