0

I'm new to Python, and I need some help. I was writing a blackjack program as homework, and I think I may have it working, but whenever I run it, it complains that I have not provided anything for 'self'. I thought I didn't have to? Here is the full code:

class BlackjackPlayer:
 '''Represents a player playing blackjack
 This is used to hold the players hand, and to decide if the player has to hit or not.'''
 def __init__(self,Deck):
 '''This constructs the class of the player.
 We need to return the players hand to the deck, get the players hand, add a card from the deck to the playters hand, and to allow the player to play like the dealer.
 In addition to all of that, we need a deck.'''
 self.Deck = Deck
 self.hand = []
 def __str__(self):
 '''This returns the value of the hand.'''
 answer = 'The cards in the hand are:' + str(self.hand)
 return(answer)
 def clean_up(self):
 '''This returns all of the player's cards back to the deck and shuffles the deck.'''
 self.Deck.extend(self.hand)
 self.hand = []
 import random
 random.shuffle(self.Deck)
 def get_value(self):
 '''This gets the value of the player's hand, and returns -1 if busted.'''
 total = 0
 for card in self.hand:
 total += card
 if total > 21:
 return(-1)
 else:
 return(self.hand)
 def hit(self):
 '''add one card from the Deck to the player's hand.'''
 self.hand.append(self.Deck[0])
 self.Deck = self.Deck[1:]
 print(self.hand)
 def play_dealer(self):
 '''This will make the player behave like the dealer.'''
 total = 0
 for card in self.hand:
 total += card
 while total < 17:
 BlackjackPlayer.hit()
 total += BlackjackPlayer[-1]
 print(self.hand)
 if self.hand > 21:
 return -1
 else:
 return total

When I run this, I get:

TypeError: get_value() missing 1 required positional arguments: 'self'

I would gladly appreciate any help, and this is my first time here, so I apologize if I broke the rules or something.

Burhan Khalid
175k20 gold badges255 silver badges292 bronze badges
asked May 22, 2013 at 5:26
5
  • When you say "When I run this", what did you do :3? Commented May 22, 2013 at 5:30
  • I compiled Python from source, so I entered ./python /blackjack.py Commented May 22, 2013 at 5:31
  • Did you make an instance of the class? i.e, did you do something like player1 = BlackjackPlayer('params') Commented May 22, 2013 at 5:32
  • This is the entire program right here, if that is what you are asking. Come to think of it, I probably am trying to cram too many things into this class. Commented May 22, 2013 at 5:35
  • @user2384528, if that's the entire program, what are you expecting to run exactly? All that does is define a class. Commented May 22, 2013 at 5:40

2 Answers 2

3

I'm not sure the problem you have is in the code you've shown since you're not actually calling get_value() anywhere in there.

It will have to do with the way you're using this class. You need to ensure that you instantiate an object for this class and use it to call the function. That way, self is prefixed automagically to the argument list.

For example:

oneEyedJim = BlackJackPlayer()
score = oneEyedJim.get_value()

On top of that, your scoring doesn't seem to take into account the fact that Aces can be soft (1) or hard (11).

answered May 22, 2013 at 5:34
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the help! Embarrassingly enough, I wasn't calling it properly. Thanks for your time and help!
0

BlackjackPlayer.hit() may be the thing causing you troubles. If you want to use functions from a class, you have to create an instance of that class. However, as you're calling a function from the class, you can simply do:

self.hit()

Also:

total += BlackjackPlayer[-1]

I don't know what you're intending here, but if you wanted to access the hand list, do:

total += self.hand[-1]
answered May 22, 2013 at 5:34

1 Comment

Thanks for the tips, they were very helpful!

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.