top of page

Set and Python

If anyone if familiar with the game Set (they release a new one daily here), I wrote a program in Python to solve a basic 3 by 4 grid. It's also sold as a card game that you can play with family and friends if you're interested in purchasing it. Basically each card has four attributes: shape, color, number, and fill. And you have to find sets within the 12 cards laid out in front of you. A set is composed of three cards where each attribute is either all the same or all different. So basically my program can find those sets when you're stuck. It's kinda tedious inputting all the information but I thought it would be fun to share it anyways! I'm not an expert in coding AT ALL. This was just a fun project :)



def main():

table = []

#populate the table

#input the cards for the first row

print("Are you stuck? Well you've come to the right place!")

print()

print("First, tell us what cards are on the table.")

print()

print("Name the cards by number, color, shape, and pattern.")

print()

print("For number, put in a '1', '2', or '3'.")

print("For color, put in 'red', 'purple', or 'green'.")

print("For shape, put in 'oval', 'diamond', or 'wave'.")

print("For fill, put in 'blank','solid', or 'striped'.")

print()

print("Okay! Let's get started!")

print()


#possible inputs

numbers = ["1","2","3"]

colors = ["red", "purple", "green"]

shapes = ["oval","diamond","wave"]

fills = ["blank","solid","striped"]

#user inputs data

for t in range(12):

card = []

print("CARD " + str(t + 1))

while True:

cardnumber = input("Number: ")

if cardnumber not in numbers:

print("Number is not valid. Try again.")

else:

card.append(cardnumber)

break

while True:

cardcolor = input("Color: ")

if cardcolor not in colors:

print("Color is not valid. Try again.")

else:

card.append(cardcolor)

break

while True:

cardshape = input("Shape: ")

if cardshape not in shapes:

print("Shape is not valid. Try again.")

else:

card.append(cardshape)

break

while True:

cardfill = input("Fill: ")

if cardfill not in fills:

print("Shape is not valid. Try again.")

else:

card.append(cardfill)

break

table.append(card)

print(table)

print()

#all possible groupings

all_trios = []


for c in range(10):

x = c

for c in range(10):

y = c + 1

for c in range(10):

z = c + 2

if z > y and y > x:

trio = [table[x],table[y],table[z]]

all_trios.append(trio)


check_num = []

check_color = []

check_shape = []

check_fill = []

for group in all_trios:

count = 0

for card in group:

if count == 0:

num1 = card[0]

col1 = card[1]

sha1 = card[2]

fill1= card[3]

if count == 1:

num2 = card[0]

col2 = card[1]

sha2 = card[2]

fill2= card[3]

if count == 2:

num3 = card[0]

col3 = card[1]

sha3 = card[2]

fill3= card[3]


#make a nested list for each variable

card_num = [num1,num2,num3]

card_color = [col1,col2,col3]

card_shape = [sha1,sha2,sha3]

card_fill = [fill1,fill2,fill3]


check_num.append(card_num)

check_color.append(card_color)

check_shape.append(card_shape)

check_fill.append(card_fill)

count = count + 1

#find the sets

good_num = []

good_col = []

good_sha = []

good_fill = []


#check the numbers

count_num = 0

for group in check_num:

if group[0] == group[1] == group[2]:

good_num.append(count_num)

if group[0] != group[1] and group[1] != group[2] and group[2] != group[0]:

good_num.append(count_num)

count_num = count_num + 1

#check the colors

count_col = 0

for group in check_color:

if group[0] == group[1] == group[2]:

good_col.append(count_col)

if group[0] != group[1] and group[1] != group[2] and group[2] != group[0]:

good_col.append(count_col)

count_col = count_col + 1

#check the shape

count_sha = 0

for group in check_shape:

if group[0] == group[1] == group[2]:

good_sha.append(count_sha)

if group[0] != group[1] and group[1] != group[2] and group[2] != group[0]:

good_sha.append(count_sha)

count_sha = count_sha + 1

#check the fill

count_fill = 0

for group in check_fill:

if group[0] == group[1] == group[2]:

good_fill.append(count_fill)

if group[0] != group[1] and group[1] != group[2] and group[2] != group[0]:

good_fill.append(count_fill)

count_fill = count_fill + 1

#determine the sets

for i in good_num:

if i in good_fill and i in good_sha and i in good_col:

print(all_trios[i])

print()

main()


Commentaires


bottom of page