私はPythonの練習をしています。 私は、ゲームのすべてのフレーズがどうなるかを定義することから始めました。 今度はこのコードを実行すると、入力が '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'は、その関数内の整数です。私はそれが方法ではないので、なぜあなたが「自己」という名前を使ったのか分かりません。 –