不支持的操作数类型为+ =:'int'和pycharm上的'str'

时间:2018-05-18 23:17:06

标签: python python-3.x

我收到的错误是+ =不是用于int和string的操作 代码是

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

问题在于PC + = DC和CC + = DC

1 个答案:

答案 0 :(得分:1)

您的cards变量是字符串列表,而不是整数。你不能在python中一起添加字符串和整数。它们是单独的类。

编辑:我假设您将PC和CC分配给0 编辑2:不要知道“Nick”变量的分配。不应该造成困难。

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

你在这里!

结果:

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