python 2.7 - __getitem__ error in black jack project -


i'm trying write blackjack game online class i'm taking. know i'll have introduce objects @ point, right i'm trying write basic code operations within object. have chemistry background, absolutely 0 computer science background.

what i'm trying pull value text value first entry in tuple, assign numerical value can start compare scores, once point of creating players. however, error, , don't know means...the code below. still noob here, trying learn!

27 def value_of_card(): 

--->28 x = deal[0] 29 30 print value_of_card()

typeerror: 'function' object has no attribute 'getitem'

import random  suits = ['spades', 'hearts', 'clubs', 'diamonds'] ranks = ['ace', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten', 'jack', 'queen', 'king']  def deal():     deck = []     suit in suits:         rank in ranks:             deck.append((rank, suit))     random.shuffle(deck)     print deck     deal = deck[0]     print deal     card in deck:         if deal in deck:             deck.remove(deal)             return deck     print deck     print deal[0]  print deal()  def value_of_card(deal):     return deal[0]      print value_of_card(deal) 

some issues:

  • you pass function reference value_of_card, instead of passing value returned function.
  • your deal function not return card, deck of remaining cards. should not return deck, return deal
  • the search (with for loop) card selected not necessary: took index 0, remove card @ index 0 (with pop(0))

here corrected script:

import random  suits = ['spades', 'hearts', 'clubs', 'diamonds'] ranks = ['ace', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten', 'jack', 'queen', 'king']  def deal():     deck = []     suit in suits:         rank in ranks:             deck.append((rank, suit))     random.shuffle(deck)     # don't need search card. @ index 0     # grab, remove , return card (not deck) in 1 go:     return deck.pop(0)  card = deal() # remember dealt card print card  def value_of_card(deal):     return deal[0]  print value_of_card(card) # pass remembered card 

and here how if made more oop:

import random  class card:     def __init__(self, rank, suit):         self.rank = rank         self.suit = suit      def __repr__(self):         return self.rank + ' of ' + self.suit  class deck:     def __init__(self):         suits = ['spades', 'hearts', 'clubs', 'diamonds']         ranks = ['ace', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten', 'jack', 'queen', 'king']         self.__deck = [card(rank, suit) suit in suits rank in ranks]      def shuffle(self):         random.shuffle(self.__deck)      def deal(self):         return self.__deck.pop()  # create deck of 52 cards deck = deck()  # shuffle deck deck.shuffle()  # pull card deck card = deck.deal()  # show card print(card) # calls __repr__ method  # ..or rank: print(card.rank) 

Comments

Popular posts from this blog

What is happening when Matlab is starting a "parallel pool"? -

angular - DownloadURL return null in below code -

php - Cannot override Laravel Spark authentication with own implementation -