By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,846 Members | 1,862 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,846 IT Pros & Developers. It's quick & easy.

How to check if a string is equal to a string in a list

P: n/a
I'm trying to make a game of "ghost", but I can't seem to check the dictionary file correctly to see if the word is located in it. How can I check to see if the variable created by the players is equal to a string in a list?
The wordcheck function is what is not working.
Expand|Select|Wrap|Line Numbers
  1.  
  2. def load_words():
  3.     '''
  4.     Loads the words from 'words.txt' into a list,
  5.     and returns the list.  'words.txt' must be located
  6.     in the same directory as this program.
  7.     '''
  8.     wordlist = []
  9.     inf = open('words.txt', 'r')
  10.     for line in inf:
  11.         w = line.strip().upper()
  12.         wordlist.append(w)
  13.     inf.close()
  14.     return wordlist
  15.  
  16.  
  17. def ghost():
  18.     wordlist = load_words()
  19.     print (len(wordlist),"words were loaded.")
  20.     wd=''
  21.     n=0
  22.     while n<3:
  23.         if n%2 ==0:
  24.             let = input("player 1 input a letter:")
  25.             wd = wd + str(let[0].lower())
  26.             n = n+1
  27.         else:
  28.             let = input("player 2 input a letter:")
  29.             wd = wd + str(let[0].lower())
  30.             n = n+1
  31.  
  32.     while n>=3:
  33.         if n%2 ==0:
  34.             let = input("player 1 input a letter:")
  35.             wd = wd + str(let[0].lower())
  36.             wordcheck(wd)
  37.             n = n+1
  38.             print(wd)
  39.         else:
  40.             let = input("player 2 input a letter:")
  41.             wd = wd + str(let[0].lower())
  42.             wordcheck(wd)
  43.             n = n+1
  44.             print(wd)
  45.  
  46. def wordcheck(wordc):
  47.     wordlist = load_words()
  48.     for word in wordlist:
  49.         count = word.count(wordc)
  50.         if count ==1:
  51.             print("lose")
  52.         if wordc == word:
  53.             print("lose")
  54. ghost()
  55.  
Oct 15 '10 #1
Share this Question
Share on Google+
1 Reply


bvdet
Expert Mod 2.5K+
P: 2,851
A few suggestions:
Make sure the case of the words in the word list and the case of the word being checked is the same (all upper or all lower case).
There is no need to load the word list every time you check a word. You should only load the word list once.
To test for membership, use the in operator:
Expand|Select|Wrap|Line Numbers
  1. >>> 'word' in ['car', 'wordy', 'bell']
  2. False
  3. >>> 'wordy' in ['car', 'wordy', 'bell']
  4. True
  5. >>> 
Return True or False from wordcheck(). Add an if/else block where you are calling wordcheck(), and take some action depending on the result:
Expand|Select|Wrap|Line Numbers
  1. if wordcheck(word):
  2.     print "%s is in the word list" % (word)
  3. else:
  4.     print "%s is not in the word list" % (word)
Oct 15 '10 #2

Post your reply

Sign in to post your reply or Sign up for a free account.