实现"卡"类



这个程序是关于定义一个类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

这些是她的批评:

  • 您将在类 Card 中定义一个deal方法:此方法将发一张牌(使用随机数生成器)。
  • 您将定义一个find_face_value方法,该方法将返回卡片对象的面值。您将从程序中删除display_face_value函数。
  • 您的deal_hand函数会将对象列表传递给display_hand函数和hand stats函数(将类 Card 中的 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__()

看起来你被要求以一种非常具体的方式执行此操作。 我也不认为这是一个好方法。 看起来您的老师一开始就调用带有__的字段私有,就好像它是一个 java 私有字段一样,请注意事实并非如此。在python中,如果你想使用java风格的getters和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()

如果我理解正确的话,我的做法有所不同,你打算发 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)

有五张牌不会在您的手中重复输出。

相关内容

  • 没有找到相关文章

最新更新