-2

I am making rock paper scissors and when I run it it says "Can't convert 'int' object to str implicitly".

import time
import random
print("Do you want to play rock, paper, scissors?")
playerscore = 0
cpuscore = 0
game = input()
game = game.upper()
while game == "SURE" or game == "YES" or game == "YEAH": # starts rock                                 paper scissors
    number = random.randint(1, 3)
    if number == 1:
        cpurpors = "SCISSORS"
    elif number == 2:
        cpurpors = "ROCK"
    elif number == 3:
        cpurpors = "PAPER"
    print("Cool! Rock, paper or scissors?")
    rpors = input()
    rpors = rpors.upper()
    print("Rock")
    time.sleep(.5)
    print("Paper")
    time.sleep(.5)
    print("Scissors")
    time.sleep(.5)
    print(cpurpors + "!")
    time.sleep(.5)
    if cpurpors == rpors:
        print("Draw!")
    elif cpurpors == "SCISSORS" and rpors == "PAPER" or cpurpors == "PAPER" and rpors == "ROCK" or cpurpors == "ROCK" and rpors == "SCISSORS":
        cpuscore = cpuscore + 1
        print("Haha, I win!")
    elif rpors == "SCISSORS" and cpurpors == "PAPER" or rpors == "PAPER" and cpurpors == "ROCK" or rpors == "ROCK" and cpurpors == "SCISSORS":
        playerscore = playerscore + 1
        print("Oh no! You win!")
    print("The scores are:")
    print("Guiseppebot: " + cpuscore)
    print(name + ": " + playerscore)
    print("Would you like to play again?")
    game = input()
    game = game.upper()
ayhan
  • 70,170
  • 20
  • 182
  • 203
  • Can you include the error output? – GreenSaber Oct 10 '17 at 19:59
  • Traceback (most recent call last): File "C:\Users\Owner\Downloads\Guiseppebot Simulator (1).py", line 128, in print("Guiseppebot: " + cpuscore) TypeError: Can't convert 'float' object to str implicitly – W Singleton Oct 10 '17 at 20:00
  • Possible duplicate of [Parse String to Float or Int](https://stackoverflow.com/questions/379906/parse-string-to-float-or-int) – Cleptus Oct 10 '17 at 22:18
  • 1
    Possible duplicate of ["Can't convert 'int' object to str implicitly" error (Python)](https://stackoverflow.com/questions/45133311/cant-convert-int-object-to-str-implicitly-error-python) – Ibo Oct 10 '17 at 22:21

4 Answers4

2

The problem is that you're adding int variables to the print statements. Try doing something like this:

print(name + ": " + str(playerscore))

In that example, you're setting playerscore to the string version of the int.

GreenSaber
  • 1,118
  • 2
  • 26
  • 53
1

Need to wrap the integers cpuscore and playerscore with str to convert them to a string, as it won't do it implicitly.

print("Guiseppebot: " + str(cpuscore))
print(name + ": " + str(playerscore))

Another nice option is using format, which does call str on objects for you so you don't have to worry about it in the future:

print("Guiseppebot: {}".format(cpuscore))
print("{}: {}".format(name, playerscore))

str is the built-in function that will convert non string objects to a string. It does this by calling the objects .__str__ method if it exists (and knows how to otherwise for objects like ints and floats.) Read more about it in the docs here https://docs.python.org/3/library/stdtypes.html#str

CasualDemon
  • 5,790
  • 2
  • 21
  • 39
  • please can you explain what str means? – W Singleton Oct 10 '17 at 20:02
  • @WSingleton sure, added to answer: `str` is the built-in function that will convert non string objects to a string. It does this by calling the objects `__str__` method if it exists (and knows how to otherwise for objects like ints and floats.) – CasualDemon Oct 10 '17 at 20:05
0

cpuscore is an integer(int) it can't be concatenated with a string. Python doesn't convert it to a string automatically("implicitly"). So you at least need to do:

print("Guiseppebot: " + str(cpuscore))

Even better/easier, in Python 3.6+, use f-strings Assuming you can put the value inside a variable into a string (integers, strings, lists, dictionaries are ok), you just put them inside curly brackets.

Here's how the line with that error looks with f-strings:

print(f"Guiseppebot: {cpuscore}")
toonarmycaptain
  • 2,093
  • 2
  • 18
  • 29
0

I'm nor sure if this is the best solution, but my approach would be to use str() to convert the integer (cpuscore) to a string.

import time
import random
name = input ("what's your name? ") #added in input for variable (name)
print("Do you want to play rock, paper, scissors?")
playerscore = 0
cpuscore = 0
game = input()
game = game.upper()
while game == "SURE" or game == "YES" or game == "YEAH": # starts rock                                 paper scissors
    number = random.randint(1, 3)
    if number == 1:
        cpurpors = "SCISSORS"
    elif number == 2:
        cpurpors = "ROCK"
    elif number == 3:
        cpurpors = "PAPER"
    print("Cool! Rock, paper or scissors?")
    rpors = input()
    rpors = rpors.upper()
    print("Rock")
    time.sleep(.5)
    print("Paper")
    time.sleep(.5)
    print("Scissors")
    time.sleep(.5)
    print(cpurpors + "!")
    time.sleep(.5)
    if cpurpors == rpors:
        print("Draw!")
    elif cpurpors == "SCISSORS" and rpors == "PAPER" or cpurpors == "PAPER" and rpors == "ROCK" or cpurpors == "ROCK" and rpors == "SCISSORS":
        cpuscore = cpuscore + 1
        print("Haha, I win!")
    elif rpors == "SCISSORS" and cpurpors == "PAPER" or rpors == "PAPER" and cpurpors == "ROCK" or rpors == "ROCK" and cpurpors == "SCISSORS":
        playerscore = playerscore + 1
        print("Oh no! You win!")
    print("The scores are:")
    print("Guiseppebot: " + str(cpuscore)) #converted cpuscore to a string
    print(name + ": " + str(playerscore)) #variable (name) wasn't declared
    print("Would you like to play again?")
    game = input()
    game = game.upper()