Scrabble单词查找和计分功能不计算?
这是我需要做的事情:
拼字游戏单词查找器:用户会输入他们手头上的字母块。你的程序会打印出可以用这些字母块组成的所有单词。然后,它会打印出得分最高的单词和总分。
这是我目前的代码:
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.
为什么它会打印出我根本没有输入的字母块?还有,为什么它没有把字母块移除,这样就不能再用一次了?
2 个回答
1
你应该说明一下你是怎么调用这些函数的,以及遇到的错误。不过光从这段代码来看,我就能发现一些问题。你在 score_word_1
这个函数里从来没有给 word
赋值。还有,txt.close
这里应该加上括号,这样才能真正调用关闭函数。
那 y
是什么呢?你从来没有给它赋值,但你却在和它进行比较。
1
line.strip
应该写成 line.strip()
。你现在是把 line 赋值给一个函数。
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