jeudi 14 mai 2020

In Python, why does the output of my random card generator print 'None'?

I'm making a playing card generator. In it, a deck of 52 cards is shuffled and a random amount of cards are drawn based on the user's choice. It seems like everything is working fine, except the most important part. Where the cards should be listed, all that's printed is "None":

See for yourself

Here's my code. Anyone have any ideas? This is driving me crazy!

import random

class Card:
    ranks = {2: '2', 3: '3', 4: '4', 5: '5', 6: '6', 7: '7', 8: '8', 9: '9', 10: '10', 11: 'Jack', 12: 'Queen', 13: 'King', 1: 'Ace'}
    suits = ['Clubs', 'Diamonds', 'Hearts', 'Spades']

    def __init__(self):
        pass 

    def string_of_card(self, num):
        x = ""
        if 1 <= num <= 13:
            x = self.ranks[num] + ' of '+self.suits[0]
        elif 14 <= num <= 26:
            x = self.ranks[(num % 13) + 1] + ' of '+self.suits[1]
        elif 27 <= num <= 39:
            x = self.ranks[(num % 13) + 1] + ' of '+self.suits[2]
        elif 40 <= num <= 52:
            x = self.ranks[(num % 13) + 1] + ' of '+self.suits[3]
        return x

class Deck:
    numbers = list(range(1, 53))


    def __init__(self):
        print('Card Dealer\n')
        self.shuffle_deck()
        print('I have shuffled a deck of 52 cards.\n')

    def shuffle_deck(self):
        random.shuffle(self.numbers)

    def count_cards(self):
        return len(self.numbers)

    def deal_card(self):
        self.shuffle_deck()
        num = random.choice(self.numbers)
        self.numbers.remove(num)
        c = Card()
        x = c.string_of_card(num)
        return

d = Deck()

n = int(input('How many cards would you like?: '))

print('\nHere are your cards: ')

for i in range(1, n + 1):
    print(d.deal_card())

print('\nThere are', d.count_cards(), 'cards left in the deck.\n')
print('Good luck!')



Aucun commentaire:

Enregistrer un commentaire