그래서 저는 Python에 대한 연습을했습니다. BlackJack 게임을 작성했습니다. 나는 게임의 모든 문구가 어떻게 갈 것인지를 정의하는 것으로 시작했다. 이제 아래에서이 코드를 실행할 때 입력이 '0'인 경우 - 카드를 더 이상 원하지 않는다는 의미로 완벽하게 실행됩니다. 그러나 입력이 '1'일 때 - 당신이 카드를 선택하려는 의미, 내가 오류 얻을 :PYTHON : AttributeError : 'int'객체에 'hand'속성이 없습니다.
Traceback (most recent call last):
File "C:/Users/Maymon/PycharmProjects/untitled4/BlackJack.py", line 1, in <module>
class blackjack(object):
File "C:/Users/Maymon/PycharmProjects/untitled4/BlackJack.py", line 34, in blackjack
player(1)
File "C:/Users/Maymon/PycharmProjects/untitled4/BlackJack.py", line 25, in player
PickCard(1)
File "C:/Users/Maymon/PycharmProjects/untitled4/BlackJack.py", line 18, in PickCard
self.hand+= random.randrange(1, 13)
AttributeError: 'int' object has no attribute 'hand'
코드 : 사전에
class blackjack(object):
#this func defines how player should react
def player(self):
#this func defines what case of technical loosing
def loser():
print("You have reached", hand , ". Which is beyond 21. Therefor, you have lost the game. Better luck next time!")
#this func is responsible for picking card issue.
def PickCard(self):
import random
x=1
while x == 1:
pick = int(raw_input("Enter 1 if you want another card, else Enter 0"))
if pick == 1:
self.hand = self.hand + random.randrange(1, 13)
else:
x=0
import random
print "Now your first card will automatically be given to you:"
hand=random.randrange(1,13)
print "hand: ", hand
PickCard(1)
print hand
if hand>21:
loser()
elif hand==21:
pass
else:
pass
player(1)
감사합니다.
'self' 그 기능의 정수 당신은 전화 때문에,'PickCard (1)'. 나는 왜 그것이 당신이 '자기'라는 이름을 사용했는지 모르겠습니다. –