jrod
jrod

Reputation: 21

Can't fix my error. TypeError: object of type 'int' has no len()

I am making a game to simulate Roshambo (rock, paper, scissors). Everything works great, except when you win a game I get an error. Here's my code.

###import the random module
import random
import time
##my function
def RockPaperScissors():
    print("This is Roshambo Simulator 2016 copyright © Jared is Cool")
    time.sleep(2.5)
    print("Your score is on the left, and the computers is on the right.")
    time.sleep(1.25)
    score = [0,0]
    print(score)
    cpu  = 1, 2, 3
### game loop! you choose your weapon
    while True:
        while True:
            choice = input("Rock(1), Paper(2), or Scissors(3)?")
            if choice == "1":
                whichOne  = 1
                break
            elif choice == "2":
                whichOne =  2
                break
            elif choice == "3":
                whichOne =  3
                break

##who wins the roshambo
        cpu = random.choice(cpu)
        if whichOne == cpu:
            print("stale!")
            print(score)
        elif (whichOne == 3 and cpu == 2) or (whichOne == 2 and cpu == 1) or (whichOne == 1 and cpu == 3):
            print("win!")
            score[0] = score[0] + 1
            print(score)
        else:
            print("lose")
            print(score)
            score[1] = score[1] + 1

RockPaperScissors()

Here is the error I keep getting.

Traceback (most recent call last):
  File "python", line 41, in <module>
  File "python", line 28, in RockPaperScissors
TypeError: object of type 'int' has no len()

Upvotes: 0

Views: 453

Answers (2)

MSeifert
MSeifert

Reputation: 152587

As already mentioned in the comments:

cpu = random.choice(cpu) here you replace your list of choices by the choice (an integer). Try cpu_choice = random.choice(cpu) and replace the following cpu-names.

But you can also do some more stuff to make it shorter:

The player choice:

while True:
    choice = input("Rock(1), Paper(2), or Scissors(3)?")
    if choice in ["1", "2", "3"]:
        whichOne = int(choice)
        break

Computer choice:

cpu_choice = random.choice(cpu)

if whichOne == cpu_choice:
    print("stale!")

elif (whichOne, cpu_choice) in [(3, 2), (2, 1), (1, 3)]:
    print("win!")
    score[0] = score[0] + 1

else:
    print("lose")
    score[1] = score[1] + 1

print(score) # Call it after the if, elif, else

Upvotes: 1

Blckknght
Blckknght

Reputation: 104702

Your issue is on this line:

cpu = random.choice(cpu)

The first time that runs, it works because cpu is assigned a tuple at the start of your code. It fails when the loop comes around again though, since you've replaced the tuple with a single value (an integer).

I suggest using a different variable for one value, so the line would be something like:

cpu_choice = random.choice(cpu)

You could alternatively use random.randint(1,3) to pick the value and not bother with the tuple at all.

Upvotes: 3

Related Questions