Mr Pizza
Mr Pizza

Reputation: 9

unsupported operand type(s) for +=: 'int' and 'str' on pycharm

I am getting the error that += is not the operation used for int and string the code is

while True:
    cards = ['2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13', '14']
    DC = random.choice(cards)
    PC += DC
    card = random.choice(cards)
    CC += DC
    again = input("again : ")
    if again == "no":
        print("Ok")
        if 21 < PC:
            print("YOU LOSS")
            break
        elif PC > CC:
            print("YOU WON")
            break
        else:
            print("YOU LOSS")
            break
    elif 21 < PC:
        print(nick, "LOSE")
        break

the problem is with PC += DC and CC += DC

Upvotes: 1

Views: 1224

Answers (1)

Aaron Brandhagen
Aaron Brandhagen

Reputation: 301

Your cards variable is a list of strings, not integers. You can't add a string and an integer together in python. They are separate classes.

Edit: I'm assuming you are assigning PC and CC to 0 Edit 2: Don;t know what the "Nick" variable is assigned to. Shouldn't make a dif.

import random
PC= 0 # <--|  not sure if they are what you are assigning them 

while True:
    cards = ['2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13', '14']
    DC = random.choice(cards)
    PC += int(DC) # <-- Notice
    card = random.choice(cards)
    CC += int(DC)# <-- Notice
    again = input("again : ")
    if again == "no":
        print("Ok")
        if 21 < PC:
            print("YOU LOSS")
            break
        elif PC > CC:
            print("YOU WON")
            break
        else:
            print("YOU LOSS")
            break
    elif 21 < PC:
        print(nick, "LOSE")
        break

Here you are!

Results:

again : >? 23
again : >? 2
again : >? 54
LOSE

Upvotes: 1

Related Questions