user7551942
user7551942

Reputation:

Python Refuses to change variable

Python will not change my variable of Ace, Jack, Queen or King, to 10 when asked, instead it seems to skip the while loop and just go along.

I'm using 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))

Upvotes: 0

Views: 97

Answers (2)

Nullman
Nullman

Reputation: 4279

There are all sorts of things wrong with this code, but consider it an opportunity to play with classes. Consider the following:

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

Now you can make cards such as:

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

Upvotes: 0

Raymond Hettinger
Raymond Hettinger

Reputation: 226181

Replace:

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

With:

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

The issue with the first line is that it didn't check to see if x1 was in the list of face cards, instead it tested whether x1 actually was a list of facecards.

The issue with the second line is that == is an equality test. You want just = which is an assignment.

Upvotes: 4

Related Questions