这是我应该做的:
Scrabble Word Finder:您的用户将输入他们当前拥有的图块。您的程序将打印字典中可由这些图块创建的所有单词。然后,它将打印具有最多拼字游戏点和总点数的单词。
到目前为止,这是我的代码:
def main():
scrabblefinder()
def scrabbleFinder():
value_list = {'a': 1, 'b': 3, 'c': 3, 'd': 2, 'e': 1, 'f': 4, 'g': 2,
'h': 4, 'i': 1, 'j': 8, 'k': 5, 'l': 1, 'm': 3, 'n': 1,
'o': 1, 'p': 3, 'q': 10, 'r': 1, 's': 1, 't': 1, 'u': 1,
'v': 4, 'w': 4, 'x': 8, 'y': 4, 'z': 10}
string1=str(input("What tiles do you have?: "))
ls1 = list(string1)
string2 = open("scrabble_wordlist_finder.txt","r")
highest = 0
higheststring = ""
points = 0
for line in string2:
ls2=list(line)
if len(string1)+1>=len(line):
#ls2.sort()
x=0
for n in ls2:
if n >= ls1[x+1]:
x+=1
else:
break
if x+1>=len(ls2):
line=line.strip();
word_index = 0
total = 0
points = sum(value_list[char] for char in line)
if points > highest:
highest = points
higheststring = line
print(str(line),",",points,"points")
print("The highest scoring word is:",higheststring,",",points,"points.")
main()
但是,如果我输入图块,例如“qwertyas”,它将打印以下内容:
we , 5 points
wert , 7 points
west , 7 points
wet , 6 points
why , 12 points
wiry , 10 points
wis , 6 points
wist , 7 points
wit , 6 points
witty , 11 points
wiz , 15 points
wo , 5 points
wort , 7 points
wos , 6 points
wost , 7 points
wot , 6 points
wow , 9 points
wry , 9 points
xi , 9 points
xis , 10 points
xu , 9 points
xyst , 14 points
ye , 5 points
yes , 6 points
yet , 6 points
yett , 7 points
yew , 9 points
you , 6 points
yow , 9 points
yurt , 7 points
zest , 13 points
zesty , 17 points
The highest scoring word is: zesty , 17 points.
为什么要打印我甚至没有输入的瓷砖?另外,为什么不删除瓷砖,以免再次使用?
答案 0 :(得分:1)
您应该包括调用函数和错误的方式,但我只能从这段代码中看到一些问题。您从未将word
设置为score_word_1
函数中的任何内容。 txt.close
应该有括号,所以它实际上调用了close函数。
什么是y
?你永远不会把它设置成任何东西,但你要比较它。
答案 1 :(得分:1)
line.strip
应为line.strip()
您正在为某个功能指定行。
In [5]: s="abc "
In [6]: s=s.strip
In [7]: s
Out[7]: <function strip>
In [9]: s="abc "
In [10]: s=s.strip()
In [11]: s
Out[11]: 'abc'
您可以使用with
打开您,因为它会自动关闭文件并列出理解来创建您的myList
:
with open("scrabble_wordlist_finder.txt", "r") as f:
myList = [line.strip() for line in f]
if y == x: # y has not been assigned a value anywhere