# Global variable for flash cards
flash_cards = {}
def read_flash_cards_from_text_file():
"""Will read line by line the card name and definition from a local txt file"""
global flash_cards
with open("/Users/falks/pyproject1/my_env/Quizlet/flash_cards.txt", "r") as f:
for lines in f:
lines_stripped = lines.strip()
index_split = lines_stripped.index("=")
key = lines_stripped[0:index_split].strip()
values = lines_stripped[index_split+1:].strip()
flash_cards[key] = values
print(flash_cards)
if len(flash_cards) < 4:
print("You must have at least 4 flash cards in text file before starting Quizlet, exiting program now")
sys.exit()
#read_flash_cards_from_text_file()
def update_flash_cards(key, values, save_to_file=True, delete_flash_card=False):
"""Will be called whenever adding or deleting or replacing a flash card"""
global flash_cards
if delete_flash_card:
del flash_cards[key]
if save_to_file:
write_flash_cards_to_text_file()
def write_flash_cards_to_text_file():
"""Will line by line write the name and definition for each flash card to a file for saving"""
global flash_cards
with open("./flash_cards.txt", "w+") as f:
for l, f in flash_cards.items():
f.write(l + "=" + f + "\n")
def add_flash_card():
"""Will be called from main menu to create or update flash card"""
global flash_cards
save_file = False
while save_file is False:
key = input("Write down your Name of the flash card: " )
values = input("write down your defintion of the card name: ")
save_to_file = input("are you sure y/n: ")
if save_to_file == "n":
continue
if save_to_file == "y":
save_to_file = True
save_file = True
return update_flash_cards(key, values, save_to_file)
read_flash_cards_from_text_file()
add_flash_card()
Can somebody pls explain me, why its running me this Error:
{'g': '5', 'f': '4', 'r': '6', 't': '12'}
Write down your Name of the flash card: w
write down your defintion of the card name: r
are you sure y/n: y
Traceback (most recent call last): File "C:\Users\falks\pyproject1\my_env\Quizlet\QuitzletTry.py", line 88, in add_flash_card() File "C:\Users\falks\pyproject1\my_env\Quizlet\QuitzletTry.py", line 77, in add_flash_card return update_flash_cards(key, values, save_to_file) File "C:\Users\falks\pyproject1\my_env\Quizlet\QuitzletTry.py", line 49, in update_flash_cards write_flash_cards_to_text_file() File "C:\Users\falks\pyproject1\my_env\Quizlet\QuitzletTry.py", line 59, in write_flash_cards_to_text_file f.write(l + "=" + f + "\n") AttributeError: 'str' object has no attribute 'write'
I dont get it, thanks for you help.it does delete the flash_cards,txt. but doesn't overwrite it.
if anyone wonders, i code a simple Quizlet programm for exercise.
The issue is here. You are using same name for both the file object and loop variable. When you call f.write(l + "=" + f + "\n")
, f
is no longer a file object(Because the the loop variable overwritten f
into a string object)
with open("./flash_cards.txt", "w+") as : for l, in flash_cards.items(): f.write(l + "=" + f + "\n")
you have f
in your for
inside the with
You may choose another variable, and the problem will be removed. As example:
with open("./flash_cards.txt", "w+") as f:
for l, value in flash_cards.items():
f.write(l + "=" + value + "\n")
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.