我是编程和 python 的新手。我在网上寻求帮助,我按照他们说的做,但我认为我犯了一个我无法理解的错误。现在我在这里要做的就是:如果单词与用户输入的长度与文件中的单词匹配,则列出这些单词。如果我userLength
用实际数字替换它有点工作,但它不适用于 variable userlength
。稍后我需要该列表来开发 Hangman。
任何关于代码的帮助或建议都会很棒。
def welcome():
print("Welcome to the Hangman: ")
userLength = input ("Please tell us how long word you want to play : ")
print(userLength)
text = open("test.txt").read()
counts = Counter([len(word.strip('?!,.')) for word in text.split()])
counts[10]
print(counts)
for wl in text.split():
if len(wl) == counts :
wordLen = len(text.split())
print (wordLen)
print(wl)
filename = open("test.txt")
lines = filename.readlines()
filename.close()
print (lines)
for line in lines:
wl = len(line)
print (wl)
if wl == userLength:
words = line
print (words)
def main ():
welcome()
main()