Last answered:

10 May 2023

Posted on:

21 Nov 2021

2

Solution to the last task

class PlayingCard(object):
    def __init__(self, value, suit):
        self.value = value
        self.suit = suit
    def suit(self):
        return self.suit
    def value(self):
        return self.value
    def __str__(self):
        suits = {'d':'Diamonds', 'h':'Hearts', 's':'Spades', 'c':'Clubs'}
        values = {1:'Ace', 2:2, 3:3, 4:4, 5:5, 6:6, 7:7, 8:8, 9:9, 10:10, 11:'Jack', 12:'Queen', 13:'King'}
        my_card = str(values[self.value]) + ' of ' + str(suits[self.suit])
        return my_card
coolest_card = PlayingCard(4, 'h')
print(coolest_card)
sheesh_card = PlayingCard(11, 's')
print(sheesh_card)

2 answers ( 0 marked as helpful)
Posted on:

08 Aug 2022

0

Anyone has idea how could it be possible to adjust code if user inputs value that doesn't match suits or values list?
Like a while or if statement?

Posted on:

10 May 2023

0

Hello Daniels, For your query regarding an invalid user input:

Please use the below code for the dunder function __str__:

def __str__(self):
        if self.value not in card.values or self.suit not in card.suits:
                return 'Invalid Input'
        else:    
                my_card = 'The selected card is ' + str(card.values[self.value]) + ' of ' + \
            str(card.suits[self.suit])

         return my_card


Submit an answer