Reputation: 79
Here is what I'm supposed to do:
Scrabble Word Finder: Your user will enter the tiles which they currently have. Your program will print all words in the dictionary which can be created by those tiles. Then, it will print the word with the most scrabble points and the point total.
Here is my code thus far:
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()
However, if I enter the tiles, for example, "qwertyas", it will print the following:
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.
Why is it printing tiles that I didn't even enter? Also, why isn't it removing tiles so they can't be used again?
Upvotes: 0
Views: 1045
Reputation: 180481
line.strip
should be line.strip()
You are assigning line to a function.
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'
You can use with
to open your as it will automatically close the file and list comprehension to create your 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
Upvotes: 1
Reputation: 11935
You should include how you are calling the functions and the errors, but I can see some problems just from this code alone. You never set word
to anything in the score_word_1
function. txt.close
should have parentheses so it actually calls the close function.
What is y
? You never set it to anything, but you're comparing it.
Upvotes: 1