I am trying to code a card game in python using OOP. the user should select either red( hearts and diamonds) or black( clubs and spades) the user then goes on with the game.
here is the code:
import random
class Card:
def __init__(self, name, suit):
self.name = name
self.suit = suit
def print_card(self):
print(self.name ' of ' self.suit)
def show(self):
print('{} of {}'.format(self.suit, self.name))
class Deck:
def __init__(self):
self.cards = []
self.build()
def build(self):
for s in ['Hearts', 'diamonds', 'Clubs', 'Spades']:
for v in range(1, 14):
self.cards.append(Card(s, v))
def shuffle(self):
random.shuffle(self.cards)
def show(self):
for c in self.cards:
c.show()
class Game:
def __init__(self, player, score):
self.player = player
self.score = score
self.cards = []
self.hand = []
def shuffle(self):
random.shuffle(self.cards)
def drawCard(self):
return self.cards.pop()
def draw(self, deck):
self.hand.append(deck.drawCard())
return self
def showHand(self):
for card in self.hand:
card.show()
def reset(self):
pass
print('Welcome message')
user = input("Please enter your name: ")
print('While you start your score is 1000')
team = input((' RED or BLACK \n Your team : '))
while True:
if team == 'red':
print('Great, You have chosen Team Red, hearts and diamonds will fetch you points, clubs and sp')
print("")
playGame = input(('Press ENTER to play'))
print('Game starts')
shuffleCard = input(('Press ENTER to shuffle and Pick your card'))
deck = Deck()
deck.shuffle()
print('That was a good mix')
showCard = input(('Press ENTER to reveal your card'))
user = Game(user, 1000)
user.showHand --> here i am expecting the final card to show up but its not happening
the terminal gives this error:
Traceback (most recent call last):
File "/Users/yoshithkotla/PycharmProjects/pythonFinalProject001/main.py", line 71, in <module>
player = player()
TypeError: 'str' object is not callable
Details of the problem : Using the OOP topics and programs we examined in our Lecture Notes 8 and 9 develop a Python program to implement a card game. You select the card game. o You can invent your own simple card game; just provide the instructions for your card game as comments in your Python program. In our Lecture Notes 9, we will be developing OOP Python code for the fundamentals of a card game. Use this code in your implementation of your card game. Add additional OOP code for your specific card game.
CodePudding user response:
Changes made:-
player=player() -> player=game()
Code:-
class game:
def __init__(self, player, score):
self.player = player
self.score = score
def start_game(self, player, score, team):
self.score = score
self.player = player
self.team = team
print("Welcome")
player = input("Please enter your name: ")
print('While you start your score is 1000')
team = input((' RED or BLACK \n Your team : '))
while team == 'red':
print('Great, You have chosen Team Red, hearts and diamonds will fetch you points, clubs and sp')
print("")
playGame = input(('Press ENTER to play'))
print('game starts')
shuffleCard = input(('Press ENTER to shuffle and Pick your card'))
deck = Deck()
deck.shuffle()
print(' that was a good mix')
showCard = input(('Press ENTER to reveal your card'))
player = game()
player.draw(deck)
player.showHand
break
Output:-
Welcome
Please enter your name: Yash
While you start your score is 1000
RED or BLACK
Your team : red
Great, You have chosen Team Red, hearts and diamonds will fetch you points, clubs and sp
Press ENTER to play
Updated query:-
I think you should go with the process to learn all the conditions for the game than start your code.. for reference you should follow this -> click here to know all the conditions for the game and then try to code..!
CodePudding user response:
In the line player = player()
you are trying to call a function named 'player' by adding brackets. Player is not a function but a string variable where you store the entered name of a player, so you cannot call it.
Try to erase the brackets or delete line completely (since you've already assigned the variable in line player = input("Please enter your name: ")
).