Python拒绝改变变量

时间:2017-02-12 00:40:45

标签: python python-3.x

当被问到时,Python不会将我的Ace,Jack,Queen或King的变量更改为10,而是似乎跳过while循环而只是继续。

我正在使用python 3.5。

Ace = "Ace"
Jack = "Jack"
Queen = "Queen"
King = "King"

x = [Ace, 2, 3, 4, 5, 6, 7, 8, 9, 10, Jack, Queen, King, Ace]
x1 = random.choice(x)   
x2 = random.choice(x)

# this array is irrelevant to the question.
suit = ["Spades", "Hearts", "Diamonds", "Clubs"]
suit1 = random.choice(suit)
suit2 = random.choice(suit)
while suit1 == suit2:
    suit2 = random.choice(suit)

Ace = "Ace"
Jack = "Jack"
Queen = "Queen"
King = "King"

while x1 == ["Jack", "Queen" , "King"]:
    x1 == 10

while x2 == ["Jack" , "Queen" , "King"]:
    x2 == 10

print ("Your cards are the " + str(x1) + " of " + str(suit1) + 
       " and the " + str(x2) + " of " + str(suit2))
print (str(x1) + " " + str(x2))

# When it tries to add the two variables here, it comes up with an error, 
# as it cannot add "King" to "10", since "King" is not a number.
total = (int(x1) + int(x2))

2 个答案:

答案 0 :(得分:4)

替换:

while x1 == ["Jack", "Queen" , "King"]:
    x1 == 10

使用:

while x1 in ["Jack", "Queen" , "King"]:
    x1 = 10

第一行的问题是它没有检查 x1 是否在面部卡列表中,而是测试了 x1 是否实际上是面子清单。

第二行的问题是==是一个相等测试。您只需要=这是一项任务。

答案 1 :(得分:0)

此代码存在各种错误,但认为这是一个与类一起玩的机会。请考虑以下事项:

class Face(object):
    def __init__(self, face, value):
        self.face = face
        self.value = value
    def __int__(self):
        return self.value
    def __str__(self):
        return self.face
    def __repr__(self):
        return self.face

现在你可以制作如下卡片:

card = Face('king',13)
card #will return 'king'
str(card) #will return 'king'
int(card) #will return 13