Python: List to .txt file doesn’t work!

Updated on September 29, 2019 in [A] Other Answers
Share on Facebook0Tweet about this on TwitterShare on Google+0Share on Reddit0
0 on September 29, 2019

Hello everyone,

I have a problem with a program, that I wrote on my own, which checks your knowledge in Vocabulary. You can create a session with as many vocabulary as you want and you can write these words and their answers and if you have finished with writing this vocabulary the program can ask you these words and you should write the solution in the other language. The words are saved in 2 lists (1 list for the first language and the other list for the solutions of every word in the other language). And everything works fine, but if you close the program, your words will not be saved. And I wanted to save these lists in .txt files. Creating a .txt file works, but I can’t insert a list into a .txt file, it should be a string. I tried the .join() method but I still get some errors. I hope someone can help me. Thank you very much!!!

 

Here are the 2 functions, where the problem is (all variables are assigned) [I wrote it in German in the original script, but I changed everything in English, so if there are some words that I forgot to translate, so there is an error for unassigned variables, it’s not the problem which I am searching for]:

And the formatting of Python in this Forum is weird, I hope you can understand what I wrote ūüôā

I know that this what I wrote in def output() doen’t make sense if you compare it with this what I wrote in def query() but otherwise nothing would work, it couldn’t write the list in the .txt file. Now the reading part is the problem, but anyhow please look at the whole thing. If you need the whole script, the print commands, input commands and some variables are in German, but I’ll post it if anyone needs it. Thanks!!!¬†

 

def beginning():
 vokabel = input("Geben Sie eine Vokabel auf " + firstLanguage + " ein: ")
 if vokabel == "":
 print("This isn't a valid value, try again!.")
 beginning()
 firstList.append(vokabel)
translation = input("What does this mean in " + secondLanguage + "? ")
 if translation == "":
 print("This isn't a valid translation.")
translation = input("What does this mean in " + secondLanguage + "? ")
 secondList.append(translation)
 def query():
 finished = input("Is that all? Enter Yes to confirm the list of vocabulary or press ENTER to add another word: ")
 if finished == "Yes":
 file = open(name + ".txt", "w")
 file.write (str("\n".join(str(listItem)) for listItem in firstList))
 file.write("\n")
 file.write(str("\n".join(str(listItem2)) for listItem2 in secondList))
 file.close()
 output()
 elif finished == "":
 beginning()
 else:
 print("This isn't a valid value, try again!")
query()
query()
 
 
 
def output():
 file = open(name + ".txt", "w")
 listFromTXT1 = file.read(firstList)
 file.close()
outputting = random.choice(listFromTXT1)
 print(outputting)
number = listFromTXT1.index(outputting)
check = input("Translation: ")
 file = open(name + ".txt", "w")
 vokabelsecondLanguage = file.read(secondList[number])
 file.close()

 

 

  • Liked by
  • ...
Reply