Skip to content

Instantly share code, notes, and snippets.

@davegotz
Created March 28, 2019 14:21
Show Gist options
  • Select an option

  • Save davegotz/1d84045d99952a5ac50ee7b3927e10ae to your computer and use it in GitHub Desktop.

Select an option

Save davegotz/1d84045d99952a5ac50ee7b3927e10ae to your computer and use it in GitHub Desktop.
# Playing Card class, represents a single playing card.
class PlayingCard:
def __init__(self, value, suit):
# Setup the instance variables.
self.value = value
self.suit = suit
self.face_up = False
def get_suit(self):
return self.suit
def get_value(self):
return self.value
# Returns a number, 1-10, corresponding to the card face value.
# Aces are 1, Royals are 10.
def get_number_value(self):
try:
return int(self.value)
except ValueError:
# This will only happen if self.value is not a number.
if self.value == "Ace":
return 1
else:
return 10
# Inverts the face up status.
def flip(self):
self.face_up = not self.face_up
def is_face_up(self):
return self.face_up
def main():
test_card = PlayingCard(3, "Spades")
another_card = PlayingCard("Ace", "Spades")
third_card = PlayingCard("King", "Spades")
# Printing card values.
print(test_card.get_value())
print(another_card.get_value())
print(third_card.get_value())
# Printing card number values
print(test_card.get_number_value())
print(another_card.get_number_value())
print(third_card.get_number_value())
# Test flipping a card.
print("Card three face up = ", third_card.is_face_up())
third_card.flip()
print("Card three face up = ", third_card.is_face_up())
third_card.flip()
print("Card three face up = ", third_card.is_face_up())
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment