954,525 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Help Python project

My project is to create a solitaire game with a different card deck.

I have created four lists with three objecs in each off them and I would like to create 81 unique cards out off these objecs.

colors = [blue, yellow, red]

shapes = [triangel, circle, square]

numbers = [1, 2, 3]

grades = [1, 2, 3]


cards = []

for color in colors:
for form in forms:
for number in numbers:
for grade in grades:
cards.push((color, form, number, grade))

If I want to randomly select three out off these 81 cards. What's the best way to do that?

/Johan

Swedenrock
Newbie Poster
9 posts since Nov 2010
Reputation Points: 10
Solved Threads: 0
 
import random
selection = random.sample(cards, 3)
Gribouillis
Posting Maven
Moderator
2,786 posts since Jul 2008
Reputation Points: 1,044
Solved Threads: 691
 

This is how my code look so far. I don't get the lists to work though

class spelkort:

   def __init__(self):
         




       colors = [blue, yellow, red]

       forms = [triangel, circle, square]

       numbers = [1, 2, 3]

       grades = [1, 2, 3]



   cards = []

   for color in colors:
      for form in forms:
         for number in numbers:
            for grade in grades:
               cards.push((color, form, number, grade))

import random
kortutfall = random.sample(cards, 6)

print kortutfall
Swedenrock
Newbie Poster
9 posts since Nov 2010
Reputation Points: 10
Solved Threads: 0
 

This one runs

colors = ["blue", "yellow", "red"]
forms = ["triangel", "circle", "square"]
numbers = [1, 2, 3]
grades = [1, 2, 3]
cards = []

for color in colors:
    for form in forms:
        for number in numbers:
            for grade in grades:
                cards.append((color, form, number, grade))

import random
kortutfall = random.sample(cards, 6)

print kortutfall

""" my output -->
[('red', 'circle', 1, 2), ('blue', 'triangel', 1, 2), ('yellow', 'square', 3, 3), ('blue', 'square', 1, 1), ('yellow', 'triangel', 3, 2), ('red', 'circle', 1, 3)]
"""


Hint: don't write code that you don't understand.

Gribouillis
Posting Maven
Moderator
2,786 posts since Jul 2008
Reputation Points: 1,044
Solved Threads: 691
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: