我已将两个文本文件导入我的程序 -
f = open("words.txt","r")
words = f.read()
f = open("solved.txt","r")
solved = f.read()
他们参与了我正在制作的“猜词”游戏。
在游戏结束时,程序会检查用户对真实答案的答案......
print('Checking words...')
sleep(2)
if (words) == (solved):
print('>Well done!')
sleep(1)
print('>All of your answers are right')
else:
print('Not quite, keep trying!')
sleep(1)
menu()
单词文本文件在字符串的末尾有一个额外的回车符,所以无论用户使用什么字符串,它都不会与解决的字符串完全相同(最后没有回车符) ,因此用户永远不会赢得比赛。
我只编辑程序中的文本文件,所以我想从字符串的末尾删除额外的回车符,以便用户赢得游戏。
Words.txt -
#+/084&"
#3*#%#+
8%203:
,1$&
!-*%
.#7&33&
#*#71%
&-&641'2
#))85
9&330*
Theres an extra space here
Solved.txt -
ACQUIRED
ALMANAC
INSULT
JOKE
HYMN
GAZELLE
AMAZON
EYEBROWS
AFFIX
VELLUM
-Alastair
答案 0 :(得分:0)
def getwords(file_name):
with open(file_name) as file:
for word in file:
word = word.strip()
if word: yield word
words = list(getwords("words.txt"))
solved = list(getwords("solved.txt"))
... # do anything with 'words' and 'solved' #...