Translator Swedish list to English list. Words are already chosen in a text file. The glossary will ask you to translate each word and show the results in the end and export them in a text file. The idea is to update the results each time you run the file by importing the old results(here I got stuck)
My code in Python:
class GlosClass:
def init(self, swedish, english,WrongRight=‘Wrong’,correct_answer=0,wrong_answer=0):
self.__swedish = swedish
self.__english = english
self.__WrongRight=WrongRight
self.__correct_answer=correct_answer
self.__wrong_answer=wrong_answer
def correct_answer(self):
self.__correct_answer+=1
def last_answer(self):
self.__WrongRight="right"
def last_answer(self):
self.__WrongRight="wrong"
def wrong_answer(self):
self.__wrong_answer+=1
def __str__(self):
answer = str(self.swedish)
answer += str(self.english)
answer += str(self.WrongRight)
answer += str(self.correct_answer)
return answer
def swedish_word(self):
return self.__swedish
def english_word(self):
return self.__english
def points(self):
return self.__correct_answer
def totalscore(self):
total=self.__correct_answer+self.__wrong_answer
return total
def your_answer(self):
return self.__WrongRight
def glos_with_Lists():
“”“This function puts the words together in separate two lists, English and Swedish lists”""
file = open("Glosor.txt", "r", encoding = "utf=8")
glosses = []
for line in file:
swedish = line.split()[0]
english = line.split()[1]
WrongRight=''
glosobject = GlosClass(swedish,english)
glosses.append(glosobject)
return glosses
def show_glos(glosses):
“”" This function makes the order, ps! Show words are long and others are short"""
print('swedish:\tenglish:\tpoints\tlast_answer')
string=""
for glos in glosses:
eng=glos.english_word()
sve=glos.swedish_word()
if len(sve)<7:
glos1= sve+'\t'+'\t'+eng+"\t\t"+str(glos.points())+" /"+str(glos.totalscore())+"\t"+str(glos.your_answer())
else:
glos1= sve+'\t'+eng+"\t\t"+str(glos.points())+" /"+str(glos.totalscore())+"\t"+str(glos.your_answer())
print(glos1)
string+=str(glos1) + "\n"
glos1 +"\n"
fil_printing(string)
return glos1
def fil_printing(e):
“”" This feature prints rescues in the file “”"
fileout=open(“Glosor copy.txt”,“w”, encoding = “utf-8”)
fileout.write(str(e))
fileout.close()
return
def glosCalling(x, y, d):
answer = input(‘Translate ’ +x +’ in English: ')
answer = answer.lower()
if answer == y:
z=1
w=‘right’
print(“right”)
else:
z=0
print(“wrong”)
return z
def loop(glosses):
a=0
b=0
for glos in glosses:
for i in range(2):
#glosCalling(x, y, d)
if glosCalling(glos.swedish_word(), glos.english_word(), glos)==1:
glos.correct_answer()
glos.last_answer()
f=1
else:
glos.wrong_answer()
glos.last_answer()
f=0
if f==1:
a+=1
return a, glosses
def total_results():
lista1 = {}
fil = open(“Glosor copy.txt”, “r”, encoding = “utf=8”)
content = fil.readlines()
for x in content:
row = x.split()
lista1.update({row[1]:row[2]})
print(lista1)
def main():
glosses = glos_with_Lists()
results,Glos_Object_List=loop(glosses)
print(‘You had ’ +str(results) +’ right.’)
show_glos(Glos_Object_List)
total_results() #help
main()