
How Not To Sail
Ep. 62: I Forgot The Grand Prize! (and S3 party recap)
Jul 29 2024
Â
The Raffle Program
Okay, in case you’re a computer geek (and/or want to critique my noob code)…here’s the two main Python files that make up the raffle:raffle.py
#! /usr/bin/python3
import csv
import random
import sys
# import sleep to show output for some time period
from time import sleep
import list_animation
import my_clear
# Import raffle file, etc.
raffleFile = open('raffle.csv')
attendees = csv.DictReader(raffleFile)
attendeeNames = []
ticketNumbers = []
# Clear screen; everything will stay top left
my_clear.clear()
# Create two ordered lists
# (ticketNumbers and attendee names)
# from .csv file
for row in attendees:
ticketNumbers.append(row['Ticket Number'])
attendeeNames.append(row['Name'])
# Spin the virtual raffle machine unless
# the user types 'q' or 'Q'
print()
print()
print('Ready to Raffle, #HowNotToSailer?')
while True:
print()
print()
print('Press \'y\' (and Enter) to spin the drum,')
answer = input('or \'q\' (and Enter) to quit. ')
print()
# answer = input()
if answer == ('q' or 'Q'):
break
# Get the number of raffle tickets
# Note: the index starts at 0 , not 1...!
listSize = len(ticketNumbers)
# Get a random number between 0 and
# the maximum number.
# NOTE: The index numbers go from ZERO
# to one less than the number of entries.
spin = random.randint(0, (listSize - 1))
list_animation.raffle_animation()
my_clear.clear() #Clear screen before next raffle
# Display the 4-digit raffle ticket number
print()
print()
print('You picked Ticket Number: ' + f'{spin:04}')
print()
print(' /----------------------\\')
print(' | |')
print(' | ' + f'{spin:04}' + ' |')
print(' | |')
print(' \----------------------/')
# Find and display the winner's name
winner = str(attendeeNames[spin])
print()
print('The winner is... ' + '\033[32;1m' + winner.upper() + '\033[0m' + '!')
# So far, this displays all the ffplay status,
# which runs the winner off the screen
# (See function definition at top)
# play('raffle_applause.mp3')
my_clear.clear()
print()
print()
print('Thanks for raffling, #HowNotToSailer!')
print()
sleep(2)
my_clear.clear()
sys.exit()
list_animation.py
#! /usr/bin/python3
import sys
# import only system from os
# from os import system, name
from time import sleep
import random
import my_hide_cursor, my_clear, my_audio
def display_animation_cell(ticketTop, ticket2ndAnd4th, ticketLeftPad, animationRow, ticketRightPad, ticketBottom, sleepTime, message):
print()
print()
print(message)
print()
print(ticketTop)
print(ticket2ndAnd4th)
print(ticketLeftPad +
animationRow[0] +
animationRow[1] +
animationRow[2] +
animationRow[3] +
animationRow[4] +
animationRow[5] +
animationRow[6] +
animationRow[7] +
animationRow[8] +
animationRow[9] +
ticketRightPad)
print(ticket2ndAnd4th)
print(ticketBottom)
sleep(sleepTime)
def raffle_animation():
animationRow = ['0', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ']
i = 0 # index counter
forward = True
x = 1 # Counter to stop animation eventually
loops = 8 # Mex number of iterations
sleepTime = .05
trailingCharacter = '-'
padding = 6 # Can't exceed 7 under current design 2024-07-21
# NOTE: this cursorSymbol not used currently.
# Instead, I'm setting a random number in each animation "cell"
cursorSymbol = '\033[31;1m' + '0' + '\033[0m' # Set active character for animation
# Graphics
ticketTop = ' /----------------------\\'
ticket2ndAnd4th = ' | |'
ticketBottom = ' \----------------------/'
ticketLeftPad = ' | '
ticketRightPad = ' |'
my_hide_cursor.hide_cursor()
# Need to figure how to get audio to play
# WHILE animaion plays...
# and without a lotta display text
#
# my_audio.playAudio()
while x < loops:
if x <= (loops/2):
message = 'Spinning the raffle thingie...'
else:
message = 'What could possibly go wrong?'
# If we are moving through the list to the right,
# but have not reached the last list item...
if i < (len(animationRow) - 1) and forward == True: my_clear.clear() # Trailing characters and spaces if i == 0: animationRow[i - 1] = ' ' else: animationRow[i - 1] = trailingCharacter if i > padding:
y=i-(padding+1)
while (y) >= 0:
animationRow[y] = ' '
y -= 1
# I'm putting a random number as the active character
animationRow[i] = '\033[32;1m' + str(random.randint(0,9)) + '\033[0m'
display_animation_cell(ticketTop, ticket2ndAnd4th, ticketLeftPad, animationRow, ticketRightPad, ticketBottom, sleepTime, message)
i += 1
# If we have reached the last list item,
# clear out the trailing characters and just
# show the cursor symbol in the last slot
elif i == (len(animationRow) - 1):
my_clear.clear()
y = i-(padding+1)
while y < (len(animationRow) - 1):
animationRow[y] = ' '
y += 1
# I'm putting a random number as the active character
animationRow[i] = '\033[32;1m' + str(random.randint(0, 9)) + '\033[0m'
display_animation_cell(ticketTop, ticket2ndAnd4th, ticketLeftPad, animationRow, ticketRightPad, ticketBottom, sleepTime, message)
i -= 1
forward = False
# back at left side....
elif i == 0 and forward == False:
my_clear.clear()
for y in range(1,(padding+2)):
animationRow[y] = ' '
# I'm putting a random number as the active character
animationRow[i] = '\033[32;1m' + str(random.randint(0, 9)) + '\033[0m'
forward = True
x += 1
# Working back toward the left
else:
my_clear.clear()
animationRow[i + 1] = trailingCharacter
if i < (len(animationRow)-(padding+1)):
animationRow[i+(padding+1)] = ' '
# I'm putting a random number as the active character
animationRow[i] = '\033[32;1m' + str(random.randint(0, 9)) + '\033[0m'
display_animation_cell(ticketTop, ticket2ndAnd4th, ticketLeftPad, animationRow, ticketRightPad, ticketBottom, sleepTime, message)
i -= 1
my_hide_cursor.show_cursor()
Podcast: Play in new window | Download








