I created a class called Cards and I need to create a function called points(self)
which returns that card and its associated points. For example the I have three lists I created,
Card_rank = ["2", "3", "4", "5", "6", "Q", "J", "K", "7", "A"]
Cardsuit = ["H", "C", "S", "D"]
points2 = ["0", "2", "3", "4", "10", "11"]
and when the function is called if the input in self is either "2" - "6" any value from card suit it will print the card with value from Card_rank
and Cardsuit
and will give 0 points from points2
list.
I have tried this:
def points(self):
if self[0] == "2":
print(self " Card points is " self.points2[0])
CodePudding user response:
It appears that you are trying to define and build a deck of cards with each card having a rank. With that in mind, you might want to utilize the "index" function to determine the associated point value with a specific card. Following is an example of what that might look like.
class Deck:
def __init__(self, name):
self.name = name
self.Card_rank = ["2", "3", "4", "5", "6", "7", "8", "9", "10", "Q", "J", "K", "A"]
self.Cardsuit = ["H", "C", "S", "D"]
self.points2 = [2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10, 11] # Usual point value for games like blackjack
def points(self, card):
index = self.Card_rank.index(card)
return self.points2[index]
dk = Deck("Card Deck")
while True:
selection = input("Enter a card to test or 'Q' to quit ")
if selection.upper() == 'Q':
break
print("The card points are", dk.points(selection))
The class was set up as a deck of cards, but the principle should be apparent. Following is some sample output at the terminal.
@Dev:~/Python_Programs/Cards$ python3 Cards.py
Enter a card to test or 'Q' to quit A
The card points are 11
Enter a card to test or 'Q' to quit J
The card points are 10
Enter a card to test or 'Q' to quit 10
The card points are 10
Enter a card to test or 'Q' to quit 8
The card points are 8
Enter a card to test or 'Q' to quit K
The card points are 10
Enter a card to test or 'Q' to quit Q
@Dev:~/Python_Programs/Cards$
You might give that a try to see if it meets the spirit of your project.
CodePudding user response:
You can define a namedtuple which describes individual cards:
from collections import namedtuple
Card = namedtuple("Card", ["suit, rank", 'points'])
then for example:
c1 = Card('H', 'Q', 11)
print(c1.points )
Yields: 11