这个程序是关于定义一个类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
这些是她的批评:
deal
方法:此方法将处理一张卡片(使用随机数生成器)。find_face_value
方法,该方法将返回卡对象的面值。您将从程序中删除display_face_value
功能。deal_hand
函数会将对象列表传递给display_hand
函数和hand stats
函数,(从类卡中将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__()
答案 0 :(得分:4)
看起来你被要求以非常具体的方式做到这一点。
我不相信这也是一种很好的方式。
看起来你老师在开始私有时用__
调用字段,好像它是一个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()
答案 1 :(得分:0)
我做的不同,如果我理解正确的话,你想要出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)
有五张牌不会在你手中重复输出。