I copied a card game code online to Python and I'm trying to recreate the popular card game of War.
import random
class Card(object):
def __init__(self, suit, val):
self.suit = suit
self.value = val
def show(self):
print("{} of {}".format(self.value, self.suit))
class Deck:
def __init__(self):
self.cards = []
self.build()
def build(self):
for s in ["Spades", "Clubs", "Diamonds", "Hearts"]:
for v in {'2' : 0, '3' : 1, '4' : 2, '5' : 3, '6' : 4, '7' : 5, '8' : 6, '9' : 7 , '10' : 8, 'Jack' : 9, 'Queen' : 10, 'King' : 11, 'Ace' : 12}:
self.cards.append(Card(s,v))
def show(self):
for c in self.cards:
c.show()
def shuffle(self):
for i in range(len(self.cards)-1, 0, -1):
r = random.randint(0, i)
self.cards[i], self.cards[r] = self.cards[r], self.cards[i]
def drawCard(self):
return self.cards.pop()
class Player:
def __init__(self, name):
self.name = name
self.hand = []
def draw(self,deck):
self.hand.append(deck.drawCard())
return self
def showHand(self):
for card in self.hand:
print(self.name, "drew a ", card.show() , "!")
deck = Deck()
deck.shuffle()
peter = Player('Peter')
peter.draw(deck)
jessica = Player('Jessica')
jessica.draw(deck)
peter.showHand()
jessica.showHand()
In the Player.show_hand()
method I want to print (name object) draws a (value of suit)
. However, instead, it prints the suit of value first, and then it says Player draws a None !
. It kind of goes like this:
9 of Clubs
Peter drew a None!
It's mostly a simple error, but I want it so that the suit of value gets printed alongside the player, like this:
Peter drew a 9 of Clubs!
This is the method that results in a runtime error:
def showHand(self):
for card in self.hand:
print(self.name, "drew a ", card.show() , "!")