我是编程和python的新手。我在网上寻找帮助,我按照他们的说法行事,但我认为我犯了一个我无法理解的错误。 现在我所要做的就是:如果单词与用户在文件中输入单词的长度相匹配,请列出这些单词。如果我用实际的号码替换userLength
,但它不适用于变量userlength
。我以后需要这个名单来开发Hang子手。python中的长度函数不能按我想要的方式工作
任何关于代码的帮助或建议都会很棒。
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()
明白了。谢谢。 – AAA 2013-05-13 12:21:04