-4

The problem I've been getting is the program should produce player 2 Won! instead of draw Can't figure it out This is a question from CIE 9618\42\O\N\2022 Even the Code from thier marking scheme(for python)produces errors Help would be apreciated the code is pasted below: Oh and i've tried to see what values are being assigned using print function but I get stuff like: <main.Card object at 0x000001FBCD177940> maybe thats the problem I have no idea how to fix it

#Part A i:
class Card:
    # __Number as Integer
    # __Colour as String
    def __init__(self, Number, Colour):
        self.Number = Number
        self.Colour = Colour
#Part A ii:
    def GetNumber(self):
        return self.Number
    def GetColour(self):
        return self.Colour
#Part A iii:
IRed = Card(1, "red")
IIRed = Card(2, "red")
IIIRed = Card(3, "red")
IVRed = Card(4, "red")
VRed = Card(5, "red")
IBlue = Card(1, "blue")
IIBlue = Card(2, "blue")
IIIBlue = Card(3, "blue")
IVBlue = Card(4, "blue")
VBlue = Card(5, "blue")
IYellow = Card(1, "yellow")
IIYellow = Card(2, "yellow")
IIIYellow = Card(3, "yellow")
IVYellow = Card(4, "yellow")
VYellow = Card(5, "yellow")
#Part B i:
class Hand:
      # __Cards as Array of Card
      # __FirstCard as Integer
      # __NumberCards as Integer
      def __init__(self, ICard, IICard, IIICard, IVCard, VCard):
        self.Cards = []
        self.Cards.append(ICard)
        self.Cards.append(IICard)
        self.Cards.append(IIICard)
        self.Cards.append(IVCard)
        self.Cards.append(VCard)
        self.FirstCard = 0
        self.NumberCards = 5

#Part b ii:
      def GetCard(self, index):
            return self.Cards[index]
#Part b iii:
Player_1 = Hand(IRed, IIRed, IIIRed, IVRed, IYellow)
Player_2 = Hand(IIYellow, IIIYellow, IVYellow, VYellow, IBlue)
#Part c i:
def CalculateValue(Player):
    Score = 0
    for index in range(0, 5):
        Player_Hand = Player.GetCard(index)
        Colour = Player_Hand.GetColour
        Numbers = Player_Hand.GetNumber
        if Numbers  == 1:
            Score = Score + 1
        if Numbers == 2:
            Score = Score + 2
        if Numbers == 3:
            Score = Score + 3
        if Numbers == 4:
            Score = Score + 4
        if Numbers == 5:
            Score = Score + 5
        if Colour == "red":
            Score = Score + 5
        if Colour == "blue":
            Score = Score + 10
        if Colour == "Yellow":
            Score = Score + 15
    return Score
#Part c ii:
Player_1Score = CalculateValue(Player_1)
Player_2Score = CalculateValue(Player_2)
if Player_1Score > Player_2Score:
          print("Player 1 Won!")
elif Player_1Score < Player_2Score:
          print("Player 2 Won!")
else:
          print("Draw")

I tried over writing builtin functions for repr and str it made it readable but wasn't able to get it to work

  • `return self.__Number.__dict__` should be `return self.__Number`. And a single underscore is enough, so `__Number` should be `_Number` (or if you respect the [Style Guide for Python Code](https://peps.python.org/pep-0008/) `_number`). – Matthias Jan 25 '23 at 12:34
  • One issue that I see, is that where you have `for index in range(0, 4):` it should actually be `for index in range(0, 5):` as each player has 5 cards and you'll want the `index` value to go from 0 to 4, which is achieved by having `range(0, 5)`. – Matt Pitkin Jan 25 '23 at 12:36
  • Dispense with all the `__`-prefixed names and the associated getters. Treat attributes as public until you have a good reason not to, at which point you can replace them with properties. – chepner Jan 25 '23 at 12:37
  • according to comments the constructor of hand should take Cards as an array, but you are passing each card as a parameter. You should create the cards in a list instead – Sembei Norimaki Jan 25 '23 at 12:38
  • For starters, `Number` and `Colour` are instance variables that can be accessed directly using dot notation - no need for these to have explicitly defined getter methods. Alternatively, you should look into the `@property` decorator. Also, to expand on what @Mattias says, you should probably avoid double underscores due to [name mangling](https://stackoverflow.com/questions/7456807/should-i-use-name-mangling-in-python) – ChaddRobertson Jan 25 '23 at 12:38
  • GetNumber and GetColour are methods (functions applied to Class instances) so should be called using `GetNumber()` in CalculateValue – user19077881 Jan 25 '23 at 12:38
  • @user19077881 TYSM! it works now – mahir ahmed Jan 25 '23 at 12:56

1 Answers1

0

Problems: -Used double underscore with attributes *removed them -for index in range(0, 4) should be for index in range(0, 5) *fixed -return self.__Number.__dict__ should be return self.__Number *fixed -GetNumber in function CalculateValue should be GetNumber() *fixed it and now the program works!! TYSM to all the comments