2013-05-22 4 views
1

저는 파이썬을 처음 사용하기 때문에 도움이 필요합니다. 나는 숙제로 블랙 잭 프로그램을 쓰고 있었는데, 나는 그 프로그램이 작동 할 때마다 그것을 실행할 때마다, 내가 '자기'를 위해 아무것도 제공하지 않았다는 불만을 제기했다. 나는 그럴 필요가 없다고 생각 했단 말인가? 나는이 프로그램을 실행할 때필수 위치 인수 : 자기

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 

, 내가 얻을 : 여기에 전체 코드는

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

내가 어떤 도움을 주셔서 감사 기꺼이 것, 이것은 여기 내 처음이다, 그래서이 파산하면 내가 사과 규칙 또는 무엇인가.

+0

"내가 이것을 실행할 때"라고 말하면 어떻게 했습니까? 3? – TerryA

+0

소스에서 파이썬을 컴파일 했으므로 ./python /blackjack.py를 입력했습니다. – Silbern

+0

클래스의 인스턴스를 만들었습니까? 즉, 'player1 = BlackjackPlayer ('params ')'와 같은 것을 했습니까? – TerryA

답변

2

실제로 문제가 아니란 점은 표시된 코드에서 실제로 이 아니기 때문에 어디서나get_value()을 호출하기 때문입니다.

이 클래스를 사용하는 방식과 관련이 있습니다. 이 클래스의 객체를 인스턴스화하고이 클래스를 사용하여 함수를 호출해야합니다. 그런 식으로 self이 인수 목록에 자동으로 접두사로 붙습니다. 예를 들어

:

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

그 꼭대기에, 당신의 점수는 고려 에이스 부드러운 될 수 있다는 사실을 고려하지 않는 것 (1) 또는 (11) 하드.

+2

+1 'One Eyed Jim'+1 –

+0

도움을 주셔서 감사합니다! 당황스럽게도 나는 그것을 적절하게 부르지 않았다. 시간과 도움에 감사드립니다! – Silbern

0

BlackjackPlayer.hit()은 문제를 일으키는 원인이 될 수 있습니다. 클래스의 함수를 사용하려면 해당 클래스의 인스턴스를 만들어야합니다.

total += BlackjackPlayer[-1] 

난 당신이 여기에 의도하는지 모르겠지만, 당신이 원하는 경우 : 또한

self.hit() 

: 당신이 클래스의 함수를 호출하고 그러나, 당신은 간단하게 할 수있는 hand 목록에 액세스하려면 다음을 수행하십시오.

total += self.hand[-1] 
+0

팁 주셔서 감사합니다, 그들은 매우 도움이되었다! – Silbern

관련 문제