Hello i Just learned about the Exec statement, so i trying to make a flash card program that saves your cards. The way that i wanna keep the cards persistent is to save a dictionary to a text file and then just use the exec statement to call forth the dictionary, append it, and rewrite the appended dictionary in that text file. Yet when I play my program it gets the execution error "descriptor 'write' requires a 'file' object but received a 'str'" heres the basic script.
what did i do wrong, and how can i fix it, excuse my poor programing skills this is my first program.
#!/usr/bin/env python
''' Python flash cards, takes uses a dictionary to store
words then repeats the flashcards'''
flashcard_list=[]
flashcard_dict={}
class Flashcard(object):
def __init__(self, front , back ):
self.front=front
self.back=back
def add_card(self):
#create a text file with a dictionary for persistant data
f= file('stack', 'w')
exec f
flashcard_dict[self.front]=self.back
flashcard_list.append(flashcard_dict)
stack=str(flashcard_list)
file.write(stack)
f.close()
def quiz(self):
#irates through each dictionary in a list
#shows the key, which is the question for each one
for key in flashcard_list:
print key.keys()
text=raw_input('whats on side 2: ')
if text==flashcard_dict[self.front]:
print 'good job'
else:
question=raw_input('try again yes or no')
if question== 'no':
print flashcard_dict[self.front]
else:
self.quiz()
front=raw_input('front')
back=raw_input('back')
Flashcard=Flashcard(front, back)
Flashcard.add_card()
Flashcard.quiz()
ps literally this is not how i am planning the final product to look, I am just starting here for test purposes.