Skip to content

Instantly share code, notes, and snippets.

Created June 10, 2012 08:11
Show Gist options
  • Save anonymous/2904451 to your computer and use it in GitHub Desktop.
Save anonymous/2904451 to your computer and use it in GitHub Desktop.
Deck
from random import choice
from copy import deepcopy
import string
def do_save(current_cards, save_file="mydeck.txt"):
filehandle = file(save_file, "w")
for card in current_cards:
filehandle.writelines(card)
filehandle.close()
print "Current card list saved."
def get_data(card_list, open_file="mydeck.txt"):
filehandle = file(open_file, "r")
card_list = filehandle.readlines()
filehandle.close()
return card_list
# print "get_data card_list:"
# print card_list
# print current_cards
def choose_card(current_cards):
card = choice(current_cards)
print card
current_cards.remove(card)
#return card
print len(current_cards)
def backup_cards(card_list, backupfile="mydeck.bak"):
do_save(card_list, backupfile)
def restore_cards(current_cards, backupfile="mydeck.bak"):
response=raw_input("Are you sure you want to overwrite the current list with the backup?")
print response
print response.lower()
if (string.lower(response) in ["yes", "y"]):
print "restore_cards test after input yes"
card_list = get_data(current_cards, backupfile)
print "Card list restored from backup"
def menu():
"""Should display options and test input, then return the command to run"""
print "Menu:"
print "--------------------------------------------------"
print "1) Draw a card"
print "2) Save cards"
print "3) Restore the Deck"
print "4) Quit"
selection = 0
while (selection not in range(1, 5)):
selection = int(raw_input("Enter your choice: "))
if (selection not in range(1, 5)):
print "That is not a valid selection."
return selection
if __name__ == '__main__':
#Main Loop
card_list = []
current_cards = []
quitvar = 0
print "Welcome to the Deck of Illusions selection program."
print "The list of cards is read in from the current deck, which can be saved."
print "Once it reads the cards in, they are saved to a backup document, and not saved"
print "unless that option is selected."
# Get cards from file and make backup var
card_list = get_data(card_list)
print "main card_list"
print card_list
current_cards = deepcopy(card_list)
while quitvar == 0:
# Commands
command = menu()
if (command == 1):
choose_card(current_cards)
elif (command == 2):
do_save(current_cards)
elif (command == 3):
restore_cards(current_cards)
else:
quitvar = 1
print "Exiting Program"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment