473,387 Members | 1,757 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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

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
1 16063
bvdet
2,851 Expert Mod 2GB
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

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

Similar topics

4
by: Hamed | last post by:
Hello How can I check if a string is empty? Thanks
7
by: zjut | last post by:
I need to implement the method : round(String name, int index) The given string maybe the every type of float type, ( the msdn given the regax is that : integral-digits]exponential-digits]) ...
35
by: pinkfloydhomer | last post by:
How do I check if a string contains (can be converted to) an int? I want to do one thing if I am parsing and integer, and another if not. /David
3
by: kathy | last post by:
I want to know what is the easy way to check if a string is a number or not? the number can be int, float, double, scientific,... what is the easy way for only interger?
2
by: Bob Johnson | last post by:
Using C#/2.0 I'm writing a small "data translator" utility app that reads data out of a MS Access database and inserts it into a SQL Server database. The source db lists a bunch of names of people...
8
by: ronrsr | last post by:
I have a single long string - I'd like to split it into a list of unique keywords. Sadly, the database wasn't designed to do this, so I must do this in Python - I'm having some trouble using the...
31
by: noagbodjivictor | last post by:
How to check if a string is empty in python? if(s == "") ??
1
by: charan3609 | last post by:
my code is something like this,i have created the list containing the following names and their address. now i need to search the list by giving a string,if the string matches the any entry in the...
1
by: (2b|!2b)==? | last post by:
I am expecting a string of this format: "id1:param1,param2;id2:param1,param2,param3;id" The tokens are seperated by semicolon ";" However each token is really a struct of the following...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.