Python Forum

Full Version: Using if statements
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I am having trouble using if statements in python. I'm working on a scrabble-like game for school and I need to make a list of words that are in a dictionary and made up from letters from the users hand. e.g.

Hand: CTHSWA
Word : WATCH

currently the user can enter any combination of letters and the program accepts it.

Code
print(f"Hand: {', '.join(hand)}")
    user_word = input("Make a word (need help? press H) ").upper()
    for l in user_word:
        word_score += scores[l]

    hand_words = get_words_from_hand(hand, anagrams_of)
    
    if user_word == 'H':
        #Below line should make sure that the hand_words are from acceptable_words which is a list of every word.
        if any(hand_words) in acceptable_words:
            print(f"Possible words: {(', '.join(hand_words))}")
            print(f"Hand: {', '.join(hand)}")
            user_word = input("Make a word: ").upper()
        
    if user_word in acceptable_words:
        for l in user_word:
            print("Your word score: "f"{word_score}")       
            word_score += scores[l]
            print("Your overall score: "f"{word_score}") 
            main(word_score)
    else:
        print("thats not a word")
        main(word_score)
word_scores
is the function that score the users word
hand_words
are anagrams made of the word
acceptable_words
is a list that includes every word

The hand is a list of max 7 letters.

thanks
NLTK is not easy to learn, but to do anagrams, it's as simple as:

Quote:>>> anagrams = nltk.Index((''.join(sorted(w)), w) for w in words)
>>> anagrams['aeilnrt']
['entrail', 'latrine', 'ratline', 'reliant', 'retinal', 'trenail']

see: https://www.nltk.org/book/ch05.html for this example,
and: https://www.nltk.org/ for NLTK package info
This line is at least part of the problem:

if any(hand_words) in acceptable_words:
The any function returns True if at least one of the items in the provided list resolves to True. Non-empty strings resolve to True, so a list of words (non-empty strings) is always going to return True. Since any(handy_words) resolves to True, this line will then check to see if True is in acceptable_words. I'm guessing it never is.

You want to check each word to see if it's in acceptable_words, and then see if any of them are. That is usually done with a list comprehension:

if any([word in acceptable_words for word in hand_words]):
Since this is homework, you may not have seen list comprehensions yet. Here's how it would look as a loop:

word_check = []
for word in hand_words:
    word_check.append(word in acceptable_words)
if any(word_check):
This is not very efficient, however. If the first word is acceptable, there's no reason to check the rest of them. So we do what is called short circuiting:

for word in hand_words:
    if word in acceptable_words:
        break
if word in acceptable_words:
If we find an acceptable word, we break and don't check the rest, and word is an acceptable word for the if statement. If we don't find any words, word is the last word, which isn't acceptable for the if statement.
Update: Program will now recognise words from acceptable_words and won't allow non-words but does allow single letters

e.g. hand - acned
'a' = acceptable - shouldn't be acceptable
'acned' = non acceptable
'dance' = acceptable

The issue was that acceptable_words was lowercase and user_word was upper.

Also, the scoring system is broken, The sum of the score of the remaining letters from hand should be subtracted from the users total score

would love some advice on how to improve and fix this code.

Thanks everyone.
(Mar-20-2019, 06:56 AM)bradystroud Wrote: [ -> ]Also, the scoring system is broken, The sum of the score of the remaining letters from hand should be subtracted from the users total score

I don't see where you are removing the letters used, so that needs to be done. I would be best if player's hand was stored as a list, but you could convert it with list() and convert it back with ''.join(). If it's in a list you can loop through the word letters and use the remove() method to take them out of the list.

Then you need to score them as you are scoring the words (I would create a separate function for scoring an arbitrary string of letters), and subtract them from the total score.

There is not enough information here to diagnose why single letters are considered acceptable. It seems it would have to be a problem with acceptable_words. I would look at the file that generates that, maybe exclude single letter words when you read it. 'a' is a word, even if it's not valid in Scrabble.