Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Using if statements
#3
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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Messages In This Thread
Using if statements - by bradystroud - Mar-19-2019, 09:13 AM
RE: Using if statements - by Larz60+ - Mar-19-2019, 03:23 PM
RE: Using if statements - by ichabod801 - Mar-19-2019, 03:40 PM
RE: Using if statements - by bradystroud - Mar-20-2019, 06:56 AM
RE: Using if statements - by ichabod801 - Mar-20-2019, 01:55 PM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020