实施“卡片”class
Implementing a `Card` class
这个程序是关于定义一个 class Card
的,它在调用时生成一张牌(只有等级,没有花色)。我做的方式与说明不同,所以现在我对这个程序应该如何工作感到非常困惑。
import random
class Card:
def __init__(self):
self.__value = 0
def deal(self):
self.__value(random.randint(1,13))
def set_value(self, value):
self.value = set_face_value
def get_value():
def set_face_value(self):
faces = {1: "Ace", 2: "two", 3: "Three", 4: "Four", 5: "Five", 6: "Six", 7: "Seven", 8: "Eight", 9: "Nine", 10: "Ten", 11: "Jack", 12: "Queen", 13: "King"}
self.face_value = faces[self.value]
def __str__(self):
return self.face_value
我不得不删除其中的一部分,因为我仍在努力弄清楚我做错了什么。这是她对我的节目的回应:
import random
class Card:
def __init__(self):
self.value = 0 # THIS SHOULD BE PRIVATE - PUT __ AS THE FIRST
# TWO CHARACTERS IN THE NAME
self.face_value = '' # YOU DO NOT SAVE THIS
# YOU SHOULD HAVE ONLY ONE DATA ATTRIBUTE
def deal(self):
self.set_value(random.randint(1,13))
# THIS METHOD DOES NOT NEED TO CALL "set_value
# IT SHOULD JUST SET THE self.__value ATTRIBUTE
def set_value(self, value):
self.value = value
self.set_face_value() # READ THE ASSIGNMENT - YOU DO NOT HAVE A
# SET FOR THE FACE VALUE
# YOU SHOULD NOT HAVE ANY SUCH METHOD IN YOUR CLASS
def set_face_value(self):
faces = {1: "Ace", 2: "two", 3: "Three", 4: "Four", 5: "Five", 6: "Six", 7: "Seven", 8: "Eight", 9: "Nine", 10: "Ten", 11: "Jack", 12: "Queen", 13: "King"}
self.face_value = faces[self.value]
# YOU SHOULD HAVE A "find_face_value" METHOD THAT RETURNS THE FACE VALUE BASED ON THE VALUE OF THE
# PRIVATE DATA ATTRIBUTE "__value"
# THIS IS NOT CORRECT
def __str__(self):
return self.face_value
这些是她的批评:
- 您将在 class 卡片中定义一个
deal
方法:此方法将发一张牌(使用随机数生成器)。
- 您将定义一个
find_face_value
方法,该方法将 return 卡片对象的面值。您将从程序中删除 display_face_value
函数。
- 您的
deal_hand
函数会将对象列表传递给 display_hand
函数和 hand stats
函数,(从 class 卡片向函数发送 5 张卡片)
所以,我的主要问题是执行 deal_hand
并显示一手 5,它将显示如下:
The 5-card hand is:
Jack
Three
Queen
Two
Seven
The average value of your hand is:
7.0
#Also it should display like this
print("Your card is", card)
英语不是我的第一语言,所以我很难理解它真正想要什么。
这是他们要求的数据属性:
Class name: Card
Data attributes: __value
Methods: __init__()
deal()
set_value()
get_value()
find_face_value()
__str__()
我做的不一样,如果我没理解错的话,你应该发5张牌。
from random import *
suits = ["Clubs", "Diamonds", "Hearts", "Spades"]
values = ["Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King"]
deck = []
for s in suits:
for v in values:
deck.append(v + " of " + s)
shuffle(deck)
hand = deck[:5]
print (hand)
你手上有五张不重复的牌输出。
看起来您被要求以非常具体的方式执行此操作。
我也不认为这是一个好方法。
看起来您的老师在开始时将 __
字段称为私有字段,就好像它是 java 私有字段一样,请注意事实并非如此。在 python 中,如果你想使用 java 风格的 getter 和 setter,你应该使用 @property
注释器。
还有许多其他可以改进的地方,由于要求,我没有这样做,希望对您有所帮助。
import random
class Card:
def __init__(self):
self.__value = 0
def deal(self):
self.__value = random.randint(1, 13)
def set_value(self, value):
self.__value = value
def get_value(self):
return self.__value
def find_face_value(self):
faces = {1: "Ace", 2: "two", 3: "Three", 4: "Four",
5: "Five", 6: "Six", 7: "Seven", 8: "Eight",
9: "Nine", 10: "Ten", 11: "Jack", 12: "Queen",
13: "King"}
return faces[self.__value]
def __str__(self):
return self.find_face_value()
def main():
card = Card()
total = 0
print("Your 5 hand card is:")
for i in range(5):
card.deal()
print("Your card is", card)
total += card.get_value()
print("The average value of your hand is:")
print(total/5)
main()
这个程序是关于定义一个 class Card
的,它在调用时生成一张牌(只有等级,没有花色)。我做的方式与说明不同,所以现在我对这个程序应该如何工作感到非常困惑。
import random
class Card:
def __init__(self):
self.__value = 0
def deal(self):
self.__value(random.randint(1,13))
def set_value(self, value):
self.value = set_face_value
def get_value():
def set_face_value(self):
faces = {1: "Ace", 2: "two", 3: "Three", 4: "Four", 5: "Five", 6: "Six", 7: "Seven", 8: "Eight", 9: "Nine", 10: "Ten", 11: "Jack", 12: "Queen", 13: "King"}
self.face_value = faces[self.value]
def __str__(self):
return self.face_value
我不得不删除其中的一部分,因为我仍在努力弄清楚我做错了什么。这是她对我的节目的回应:
import random
class Card:
def __init__(self):
self.value = 0 # THIS SHOULD BE PRIVATE - PUT __ AS THE FIRST
# TWO CHARACTERS IN THE NAME
self.face_value = '' # YOU DO NOT SAVE THIS
# YOU SHOULD HAVE ONLY ONE DATA ATTRIBUTE
def deal(self):
self.set_value(random.randint(1,13))
# THIS METHOD DOES NOT NEED TO CALL "set_value
# IT SHOULD JUST SET THE self.__value ATTRIBUTE
def set_value(self, value):
self.value = value
self.set_face_value() # READ THE ASSIGNMENT - YOU DO NOT HAVE A
# SET FOR THE FACE VALUE
# YOU SHOULD NOT HAVE ANY SUCH METHOD IN YOUR CLASS
def set_face_value(self):
faces = {1: "Ace", 2: "two", 3: "Three", 4: "Four", 5: "Five", 6: "Six", 7: "Seven", 8: "Eight", 9: "Nine", 10: "Ten", 11: "Jack", 12: "Queen", 13: "King"}
self.face_value = faces[self.value]
# YOU SHOULD HAVE A "find_face_value" METHOD THAT RETURNS THE FACE VALUE BASED ON THE VALUE OF THE
# PRIVATE DATA ATTRIBUTE "__value"
# THIS IS NOT CORRECT
def __str__(self):
return self.face_value
这些是她的批评:
- 您将在 class 卡片中定义一个
deal
方法:此方法将发一张牌(使用随机数生成器)。 - 您将定义一个
find_face_value
方法,该方法将 return 卡片对象的面值。您将从程序中删除display_face_value
函数。 - 您的
deal_hand
函数会将对象列表传递给display_hand
函数和hand stats
函数,(从 class 卡片向函数发送 5 张卡片)
所以,我的主要问题是执行 deal_hand
并显示一手 5,它将显示如下:
The 5-card hand is:
Jack
Three
Queen
Two
Seven
The average value of your hand is:
7.0
#Also it should display like this
print("Your card is", card)
英语不是我的第一语言,所以我很难理解它真正想要什么。
这是他们要求的数据属性:
Class name: Card
Data attributes: __value
Methods: __init__()
deal()
set_value()
get_value()
find_face_value()
__str__()
我做的不一样,如果我没理解错的话,你应该发5张牌。
from random import *
suits = ["Clubs", "Diamonds", "Hearts", "Spades"]
values = ["Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King"]
deck = []
for s in suits:
for v in values:
deck.append(v + " of " + s)
shuffle(deck)
hand = deck[:5]
print (hand)
你手上有五张不重复的牌输出。
看起来您被要求以非常具体的方式执行此操作。
我也不认为这是一个好方法。
看起来您的老师在开始时将 __
字段称为私有字段,就好像它是 java 私有字段一样,请注意事实并非如此。在 python 中,如果你想使用 java 风格的 getter 和 setter,你应该使用 @property
注释器。
还有许多其他可以改进的地方,由于要求,我没有这样做,希望对您有所帮助。
import random
class Card:
def __init__(self):
self.__value = 0
def deal(self):
self.__value = random.randint(1, 13)
def set_value(self, value):
self.__value = value
def get_value(self):
return self.__value
def find_face_value(self):
faces = {1: "Ace", 2: "two", 3: "Three", 4: "Four",
5: "Five", 6: "Six", 7: "Seven", 8: "Eight",
9: "Nine", 10: "Ten", 11: "Jack", 12: "Queen",
13: "King"}
return faces[self.__value]
def __str__(self):
return self.find_face_value()
def main():
card = Card()
total = 0
print("Your 5 hand card is:")
for i in range(5):
card.deal()
print("Your card is", card)
total += card.get_value()
print("The average value of your hand is:")
print(total/5)
main()