-2

Hi I am really a totally beginner any help or advice is much appreciated I am trying to study the code below but when I execute the code I got nothing I tried what I can but it gives me lot of error.Kindly take a look and any advice thank you very much

import random

ranks = ['2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K', 'A']
suits = ['Hearts', 'Clubs', 'Spades', 'Diamonds']
class Card:
    def c1(self, rank, suit):
        self.rank = rank
        self.suit = suit
    def value(self):
        if self.rank in ['J', 'Q', 'K']:
            return 10
        elif self.rank == 'A':
            return 1,11
        else:
            return int(self.rank)
    def c2_(self):
        return self.rank + '-' + self.suit
class Deck:
    def d1(self):
       self.cards = []
        for rank in ranks:
            for suit in suits:
               c = Card(rank, suit)
               self.cards.append(c)
    def shuffle(self):
       random.shuffle(self.cards)
   def draw_card(self):
       if not self.cards:
            raise Exception("No more cards: empty deck!")
        card = self.cards.pop()
        return card
    def d2(self):
        cards = []
        for c in self.cards:
            cards.append(str(c))
        return str(cards)
    def test2(self):
       deck = Deck()
       print 
       print deck
       deck.shuffle()
       print 
       print deck

tryss= Deck()
tryss.d1()
tryss.draw_card()
tryss.dshuffle()
tryss.d2()
tryss.test2()
Aika
  • 21
  • 10
  • What isn't working? – Pike D. Mar 22 '17 at 23:12
  • Well, yeah. This code doesn't actually do anything by itself. I don't think there's any actual blackjack implementation here, and there's nothing that would cause a blackjack game to start when the code is run. It just defines some variables and classes. – user2357112 Mar 22 '17 at 23:13
  • (1) For your program, read the error messages. Fix the inconsistent indentation: statements at the same level have to line up vertically. (2) To get help here, you need to post according to the guidelines. This includes a full problem description, including the entire error message. – Prune Mar 22 '17 at 23:13

2 Answers2

1

As stated by others, you have a decent amount of indentation errors, those you will have to chase down.

You main issue you will run into is you aren't initializing the classes with anything. If you change the c1 and d1 functions to __init__, it will initialize them to be a class item that is then accessible with self.

Otherwise, for your prints in test2, don't print the class item by doing print(deck), but print the value using deck.d2() since that exists to stringify the cards in the deck.

The function d2 also has an error in that it should be str(c.c2_()) since that is your function to stringify the card name.

You still have a lot to get a Blackjack game out of it, but that should at least get the current code close to functional. If you have a clear description of what is not working, then please revise the question to ask that.

-1

You have way too many problems with your program:

  • indentation
  • some instance variables passed as parameteres to other methods in the same class.
  • in Card you define a method called c1, but with it you mean the constructor of the class.
  • method d1 in Deck was also a constructor. So, I erased it from your global calls
  • to print an object, you must define its string representation. Look at How to create a custom string representation for a class object?

Besides that, you're doing print without an argument. In that case, you can just do print('').

Here is your code, with proper indentation, and the print statements to work with both Python2 and Python3.

import random

ranks = ['2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K', 'A']
suits = ['Hearts', 'Clubs', 'Spades', 'Diamonds']
class Card:
    def __init__(self, rank, suit):
        self.rank = rank
        self.suit = suit
    def value(self):
        if self.rank in ['J', 'Q', 'K']:
            return 10
        elif self.rank == 'A':
            return 1,11
        else:
            return int(self.rank)
    def c2_(self):
        return self.rank + '-' + self.suit
class Deck:
    def __init__(self):
        self.cards = []
        for rank in ranks:
            for suit in suits:
                c = Card(rank, suit)
                self.cards.append(c)
    def shuffle(self):
        random.shuffle(self.cards)
    def draw_card(self):
        if not self.cards:
            raise Exception("No more cards: empty deck!")
        card = self.cards.pop()
        return card
    def d2(self):
        cards = []
        for c in self.cards:
            cards.append(str(c))
        return str(cards)
    def test2(self):
        deck = Deck()
        print('')
        print(deck)
        deck.shuffle()
        print('')
        print(deck)

if __name__ == '__main__':
    tryss= Deck()
    tryss.draw_card()
    tryss.shuffle()
    tryss.d2()
    tryss.test2()
Community
  • 1
  • 1
Nicolás Ozimica
  • 9,481
  • 5
  • 38
  • 51
  • Note to OP: This still doesn't print anything, because your main program doesn't call anything in the classes. It just defines the two lists, defines the classes, and quits without using all that work. – Prune Mar 22 '17 at 23:16
  • I updated my answer. Your code has many errors, and you must learn how to understand the error codes and messages Python gives you. – Nicolás Ozimica Mar 22 '17 at 23:41