python - Delete a line from imported text file string -
i have imported 2 text files program-
f = open("words.txt","r") words = f.read() f = open("solved.txt","r") solved = f.read()
and involved in 'guess word' game making.
at end of game, program checks users answers against real ones...
print('checking words...') sleep(2) if (words) == (solved): print('>well done!') sleep(1) print('>all of answers right') else: print('not quite, keep trying!') sleep(1) menu()
the words text file has carriage return on end of string, no matter user makes words string, never same solved string (with no carriage return on end), , therefore user can never win game.
i have edit text files within program only, way delete carriage return end of words string, user can win game.
words.txt-
#+/084&" #3*#%#+ 8%203: ,1$& !-*% .#7&33& #*#71% &-&641'2 #))85 9&330* theres space here
solved.txt-
acquired almanac insult joke hymn gazelle amazon eyebrows affix vellum
-alastair
def getwords(file_name): open(file_name) file: word in file: word = word.strip() if word: yield word words = list(getwords("words.txt")) solved = list(getwords("solved.txt")) ... # 'words' , 'solved' #...
Comments
Post a Comment