Python: A program help you to recite

2019-12-18 Views Programming | Design | Python296字2 min read
featureimg

We always need to recite something. But directly looking at the words with the definition nearby is not a good way to recite. Thus, I write this program to randomly assign the order of the words and put the definition of each word at the bottom of the file to help us recite easier.

'''
Before useing this program, please make sure you have put the source.txt in the folder.
At the same time, please make sure the txt file that you want to randomly assign have adjusted to this format:
text (symbol like -) explanation

Results will be output in the result.txt.
If you want to split the work and sentence independently, you can place a symbol you choose at the first line of source.txt. If there is not any symbol at the first line, the program will automatically use - as a symbol to split the text. [The symbol must only have length 1]
'''

import random

file = open("source.txt", "r")
text = file.read().splitlines()
sym = text[0]
flag = 0
re = []
ans = []

if len(sym) > 1:
    sym = "-"
else:
    flag = 1

dic = {}

for i in range(flag,len(text)):
    temp = text[i]
    words = temp.split(sym)
    for i in range(0,2):
        words[i] = words[i].strip()
    dic[words[0]] = words[1]
    re.append(words[0])

random.shuffle(re)

fwrite = open("result.txt","w")

for i in range(0,len(re)):
    fwrite.write(str(re[i])+"\n")

for i in range(0,8):
    fwrite.write("\n")

fwrite.write("Answers:\n")

for i in range(0,len(re)):
    if i == len(re) - 1:
        fwrite.write(dic[re[i]])
        break
    fwrite.write(dic[re[i]]+"\n")

fwrite.close()

EOF
本文章采用CC BY-NC-SA 4.0进行许可。转载请注明出处!
上一篇

Python: Gridea posts change tags


下一篇

Design: Cover image