added generate_card.py

This commit is contained in:
Tamservo 2023-11-05 07:36:06 -05:00
parent 27977a88b9
commit 8f2018b585

154
generate_card.py Normal file
View File

@ -0,0 +1,154 @@
# This is a comment. Comments begin with a '#'
# To run this program, call:
# python3 generate_card.py
# To save the output, call:'
# python3 generate_card.py > card.txt
# This will save the output in a file named 'card.txt' that you can print.
# You can name the file anything you want.
# print a 5 x 5 bingo grid with words
# text file format
#-----------
#| | | | | |
#-----------
#| | | | | |
#-----------
#| | |x| | |
#-----------
#| | | | | |
#-----------
#| | | | | |
#-----------
# This is an import. This is how we can pull in code from modules
# that have already been written
import random
import textwrap
# We will set some constant values that will not change but which multiple
# functions will use.
# They are called constants because their value will not change. Using
# all caps is how we name constants.
SQUARE_HEIGHT = 5
SQUARE_LENGTH = 14
SQUARES_PER_ROW = 5
ROWS = 5
# This is assigning an array of "strings" to the variable squares.
# We will pull values for the bingo squares from here.
# A 'string' is a series of characters enclosed in quotes
squares = [
"Proposal!",
"Secret Revealed!",
"Christmas Party",
"Mistletoe!",
"Unexpected Small Town Visit",
"Leads are Trapped Together",
"Lead Who Works as Baker or Florist",
"Last Minute Rescue",
"Amicable Break Up",
"Mistaken Identity",
"Christmas-Themed Character or Town Name",
"Green Sweater",
"A Contest",
"Surprise Relative",
"Sparkling Wine",
"Snippy Comment about Lack of Kids",
"Someone Gets a Second Chance",
"Cute Moppet",
"Giant Candy Cane",
"Christmas Miracle",
"Christmas Tree or Lights Lighting",
"LGBTQ Character",
"Neglectful OG Love Interest",
"Meet Cute!",
"Fainting or Feeling Sick",
"BIPOC Person has a Lead Role",
"Christmas Dance",
"Inappropriate High Heels or Outfit",
"We Should Have Had Sex but Halmark",
"Santa Visit",
"Lead Who Works in Marketing or Event Planning",
"Love Interest is Uber Wealthy or Royalty"
]
# This is a function. Functions are defined with the 'def' keyword.
# def 'function_name' ('parameters')
# If there are no parameters, use an empty ()
# functions make it easier to do the same thing over again.
def print_dash_line (): # Python uses indentation to organize code
print("+", end='')
for i in range(SQUARES_PER_ROW):
for j in range(SQUARE_LENGTH):
print("-", end='')
print("+", end='')
print("\n",end='') # Here we are still inside the function, but outside
# the loop
# This function will take the row of the square, and the string and
# print out the padded section of the string for this line
def get_square_text(row, s):
wrapped_strings = textwrap.wrap(str(s),width=SQUARE_LENGTH)
print("|", end='')
string_section = "test string".center(SQUARE_LENGTH)
if row < len(wrapped_strings):
string_section = wrapped_strings[row]
else:
string_section = " "
return string_section.center(SQUARE_LENGTH)
# The function print_row will take 5 strings and print a bingo row
def print_row (row_strings):
# each square will be 5 rows high and 12 chars across. We can adjust
# this to make the layout better
for i in range(SQUARE_HEIGHT):
#For each line in the row, get the part of the string
#Which will be printed on this line
for j in range(SQUARES_PER_ROW):
print( get_square_text(i, row_strings[j]), end='')
print("|")
# At random, pull values from "squares" and print out 5 rows of bingo card
selected = [-1]
for i in range(ROWS):
print_dash_line()
# get 5 new picks
picks = []
for j in range( SQUARES_PER_ROW ):
# If we have the center square, set "Free"
if i == 2 and j == 2:
picks.append("Free")
continue
# Keep picking a number until we get one we haven't picked yet
pick = -1
while pick in selected:
pick = random.randrange(0,len(squares))
# Record that we have picked this item
selected.append(pick)
picks.append(squares[pick])
# Now print the row with those picks
print_row(picks)
# Print last bottom line
print_dash_line()