实施Card类 [英] Implementing a `Card` class
问题描述
该程序是关于定义类Card
的,该类在每次调用时都会生成卡(仅排名,不适用).我用的方法与说明不同,所以现在我对程序的工作原理感到非常困惑.
This program is about defining a class Card
that generates a card whenever it is called (rank only, no suit). I did it a different way than the instructions so now I'm extremely confused on how this program is suppose to work.
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
我不得不删除它,因为我仍在努力弄清楚自己做错了什么.这是她对我的计划的答复:
I had to erase so of it because I'm still trying to figure what I did wrong. This is her response for my program:
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张卡片)
- You will define a
deal
method in your class Card: this method will deal one card (using the random number generator). - You will define a
find_face_value
method that will return the face value for the card object. You will remove thedisplay_face_value
function from your program. - Your
deal_hand
function will pass the list of objects to thedisplay_hand
function and thehand stats
function, (send 5 cards from the class Card to the function)
所以,我的主要问题是执行deal_hand
并显示5手,它将显示如下:
So, my main problems is executing deal_hand
and showing a hand of 5 and it will show like this:
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)
英语不是我的母语,所以我很难理解它的真正含义.
这是他们要求的数据属性:
English is not my first language so is hard for me to understand what it really wants.
This is the Data attributes they are asking:
Class name: Card
Data attributes: __value
Methods: __init__()
deal()
set_value()
get_value()
find_face_value()
__str__()
推荐答案
似乎要求您以非常特定的方式执行此操作.
我也不认为这是个好方法.
看起来您像老师在开始的private字段中调用了__
的字段,就好像它是java的private字段一样,请注意并非如此.在python中,如果要使用Java样式的getter和setters,则应使用@property
注释器.
还有许多其他可以改进的地方,由于要求我还没有做,希望对您有所帮助.
It looks like you are being asked to do this in a very specific way.
I don't believe it is a good way of doing it either.
It looks like you teacher calls fields with __
at the start private, as if it was a java private field, be aware this is not the case. In python, if you want to use java style getters and setters you should use the @property
annotator.
There are many other improvements that could be made, I have not done this due to the requirements, I hope this helps.
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()
这篇关于实施Card类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!