0

I want to create a list of string elements that is a deck of cards.

I created two lists of strings (1-Ace and suites). I want to use nested for loops to do this, and include a word in-between the two (" of "). I did this quickly and easily when printing everything, but when I went to append to another list (deck) I've run into problems creating the string variable that I want.

EDIT: I'm receiving the following error: "inconsistent use of tabs and indentation".
I'm currently using notepad++, but I'd gotten the same error earlier when I was using the IDLE.

nums = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King", "Ace"]
suite = ["Diamonds", "Hearts", "Clubs", "Spades"]
deck = []

for element in nums:
    for suit in suite:
        card = "%s of %s" % (element, suit)
        deck.append(card)
        print (deck)

My goal is to have a list of strings similar to this ... ["1 of hearts", "Ace of spades"]...

Thanks!

2
  • The code is fine the way it is. Sounds like your IDE is tripping you up. I would recommend upgrading if you're using the standard IDLE. Commented Jun 3, 2016 at 2:19
  • Yeah, that's what I've concluded, too. Kind of a pain, but more annoying than anything. Thanks! Commented Jun 3, 2016 at 2:23

1 Answer 1

4

The error points to improper mixing of tabs and spaces. Most text editors offer an option 'replace tabs by spaces' or something similar. Or, if you plan to stick with Python, I strongly recommend using a serious IDE like PyCharm (my favourite) or Eclipse/PyDev.

Other than that, there is nothing wrong with the code you posted. itertools.product is your friend if you want to ease the task though (and avoid indentation altogether):

from itertools import product

deck = [' of '.join(p) for p in product(nums, suite)]

Or even shorter with the help of map:

deck = map(' of '.join, product(nums, suite))
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks, but could you tell me how to make this work the way I'm doing it? I feel like it should be very simple, but I'm getting the error: "inconsistent use of tabs and indentation". I just want to know why!
Sounds like you are mixing tabs and spaces for your indentation. Better use a proper IDE to take care of proper formatting. Edit: there is nothing wrong with your code.
@AmericanMade You should put that error message in your post!
Just did! Thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.