Implementing a `Card` class

我与影子孤独终老i 提交于 2019-12-11 07:32:07

问题


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

And these are her criticisms:

  • 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 the display_face_value function from your program.
  • Your deal_hand function will pass the list of objects to the display_hand function and the hand stats function, (send 5 cards from the class Card to the function)

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__()

回答1:


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()



回答2:


I did it differently, if I understood correctly, your suppose to deal 5 cards out.

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)

There are five cards which do not repeat in your hand for the output.



来源:https://stackoverflow.com/questions/50183928/implementing-a-card-class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!