Reputation: 11
I'm sorta new to python, which you can probably see by my crappy coding below. If you were to play this game, you would probably find that it's really hard, so to make it easier, I want to make it so it only gives the user 5 letter words or less from the list. My current code for that is:
word_site = "http://www.instructables.com/files/orig/FLU/YE8L/H82UHPR8/FLUYE8LH82UHPR8.txt"
response = urllib2.urlopen(word_site)
txt = response.read()
WORDS = txt.splitlines()
randomword=(random.choice(WORDS))
Currently the code above gets any random word from that word list, I would like it to only get words that have 5 or less letters in them.
Here is all my code below, if it helps you:
import random
import urllib2
loop=True
while loop==True:
name=raw_input("Hello there! What might your name be? ")
yesorno=raw_input("Hello " + name + ", would you like to play a friendly game of Guess the Word? Answer either 'yes' or 'no': ")
if yesorno == "yes":
loop=False
print "Ok then, here we go!"
print "\n"
print "You have 3 guesses"
word_site = "http://www.instructables.com/files/orig/FLU/YE8L/H82UHPR8/FLUYE8LH82UHPR8.txt"
response = urllib2.urlopen(word_site)
txt = response.read()
WORDS = txt.splitlines()
randomword=(random.choice(WORDS))
lengthrandomword=len(randomword)
print "I'm thinking of a word with " + str(lengthrandomword) + " letters"
print "\n"
if lengthrandomword==2:
print "_ _"
print "\n"
elif lengthrandomword==3:
print "_ _ _"
print "\n"
elif lengthrandomword==4:
print "_ _ _ _"
print "\n"
elif lengthrandomword==5:
print "_ _ _ _ _"
print "\n"
elif lengthrandomword==6:
print "_ _ _ _ _ _"
print "\n"
elif lengthrandomword==7:
print "_ _ _ _ _ _ _"
print "\n"
elif lengthrandomword==8:
print "_ _ _ _ _ _ _ _"
print "\n"
elif lengthrandomword==9:
print "_ _ _ _ _ _ _ _ _"
print "\n"
elif lengthrandomword==10:
print "_ _ _ _ _ _ _ _ _ _"
print "\n"
elif lengthrandomword==11:
print "_ _ _ _ _ _ _ _ _ _ _"
print "\n"
elif lengthrandomword==12:
print "_ _ _ _ _ _ _ _ _ _ _ _"
print "\n"
elif lengthrandomword==13:
print "_ _ _ _ _ _ _ _ _ _ _ _ _"
print "\n"
elif lengthrandomword==14:
print "_ _ _ _ _ _ _ _ _ _ _ _ _ _"
print "\n"
elif lengthrandomword==15:
print "_ _ _ _ _ _ _ _ _ _ _ _ _ _ _"
print "\n"
else:
print "I thought of a word and it's reeeaaaaaly long, you would never guess it, so I'm just gonna tell you that my word was " + randomword
print "\n"
userguess=raw_input("Try and guess the word: ")
input = randomword
output = ""
for i in input.split():
output += i[0]
firstletter=output
if randomword.endswith("a"):
lastletter="a"
elif randomword.endswith("b"):
lastletter="b"
elif randomword.endswith("c"):
lastletter="c"
elif randomword.endswith("d"):
lastletter="d"
elif randomword.endswith("e"):
lastletter="e"
elif randomword.endswith("f"):
lastletter="f"
elif randomword.endswith("g"):
lastletter="g"
elif randomword.endswith("h"):
lastletter="h"
elif randomword.endswith("i"):
lastletter="i"
elif randomword.endswith("j"):
lastletter="j"
elif randomword.endswith("k"):
lastletter="k"
elif randomword.endswith("l"):
lastletter="l"
elif randomword.endswith("m"):
lastletter="m"
elif randomword.endswith("n"):
lastletter="n"
elif randomword.endswith("o"):
lastletter="o"
elif randomword.endswith("p"):
lastletter="p"
elif randomword.endswith("q"):
lastletter="q"
elif randomword.endswith("r"):
lastletter="r"
elif randomword.endswith("s"):
lastletter="s"
elif randomword.endswith("t"):
lastletter="t"
elif randomword.endswith("u"):
lastletter="u"
elif randomword.endswith("v"):
lastletter="v"
elif randomword.endswith("w"):
lastletter="w"
elif randomword.endswith("x"):
lastletter="x"
elif randomword.endswith("y"):
lastletter="y"
elif randomword.endswith("z"):
lastletter="z"
else:
lastletter="I CODED SOMETHING WRONG!"
if userguess != randomword:
print "You guessed wrong..."
print "\n"
print "I'll give you a hint: the word STARTS with the letter " + firstletter + " and ENDS with the letter " + lastletter
userguess=raw_input("I'll give you another chance to guess the word: ")
if userguess != randomword:
input = randomword
output = ""
for i in input.split():
output += i[1]
secondletter=output
print "You guessed wrong..."
print "\n"
print "I'll give you another hint: the second letter of the word is " + secondletter
userguess=raw_input("I'll give you one last chance to guess the word: ")
if userguess != randomword:
print "You guessed wrong..."
print "\n"
print "You're out of chances... the word was: " + randomword
print "I'm sorry to say this but..."
print "YOU LOST!"
print "\n"
elif userguess == randomword:
print "You guessed right!"
print "YOU WON!"
print "\n"
else:
print "I made a mistake in the code somewhere?!"
elif userguess == randomword:
print "You guessed right!"
print "YOU WON!"
print "/n"
else:
print "I made a mistake in the code somewhere?!"
mindchange=raw_input("Wanna play again? If you do, just type 'Start'. If you don't then type 'Exit': ")
if mindchange=="Exit":
loop=False
elif mindchange=="Start":
loop=True
else:
print "You didn't type either 'Start' or 'Exit' : remember to include the Capitals!"
print "\n"
elif yesorno == "no":
mindchange=raw_input("Well that's too bad, I was looking forward to beating you... well if you change your mind just type 'Start' but if you still don't want to play just type 'Exit':")
if mindchange=="Exit":
loop=False
elif mindchange=="Start":
loop=True
else:
print "You didn't type either 'no' or 'yes' : remember to not use capitals! "
print "\n"
This is what is returned:
Hello there! What might your name be? Alex
Hello Alex, would you like to play a friendly game of Guess the Word? Answer either 'yes' or 'no': yes
Ok then, here we go!
You have 3 guesses
I'm thinking of a word with 14 letters
_ _ _ _ _ _ _ _ _ _ _ _ _ _
Try and guess the word: SOMEWORD
You guessed wrong...
I'll give you a hint: the word STARTS with the letter o and ENDS with the letter g
I'll give you another chance to guess the word: SOMEOTHERWORD
You guessed wrong...
I'll give you another hint: the second letter of the word is v
I'll give you one last chance to guess the word: SOMEOTHERWORD
You guessed wrong...
You're out of chances... the word was: overregulating
I'm sorry to say this but...
YOU LOST!
Wanna play again? If you do, just type 'Start'. If you don't then type 'Exit':
If you could also point out where I've been inefficient (which is probably everywhere) as well, that would be appreciated.
Upvotes: 0
Views: 5971
Reputation: 689
I would personally use regex, particularly the re.findall() function in Python, which is fantastic. It can parse through the entire dataset almost instantly and return a super handy list.. something like this. Regex may be intimidating starting out but with a little practice, Python makes it easy and fun.
import re
with open('word_list.txt') as input_file:
data = input_file.read()
word_list = re.findall(r'\b\w{1,5}\b', data)
sample_list = word_list[0:10]
print('Total 3-5 Letter Words: {}'.format(len(word_list)))
print(sample_list)
Results:
Total 3-5 Letter Words: 12937
['aa', 'aah', 'aahed', 'aahs', 'aal', 'aalii', 'aals', 'aas', 'aba', 'abaca']
The regex matches:
\b
, which is a word boundary. In this case, it matches the beginning of a word.
\w{1,5}
matches one to 5 characters in a given word (a-z, A-Z).
\b
is used at the end as another word boundary to end the search.
Now just take that list and do what you want with it!
Upvotes: 0
Reputation: 18940
Also, you can get rid of that repeated block of if
s:
if randomword.endswith("a"):
lastletter="a"
elif randomword.endswith("b"):
lastletter="b"
elif randomword.endswith("c"):
lastletter="c"
elif randomword.endswith("d"):
lastletter="d"
elif randomword.endswith("e"):
lastletter="e"
...
elif randomword.endswith("z"):
lastletter="z"
else:
lastletter="I CODED SOMETHING WRONG!"
and just get the last letter:
lastletter = randomword[-1]
if not lastletter.isalpha():
lastletter="I CODED SOMETHING WRONG!"
The same applies for that other repeated block:
if lengthrandomword==2:
print "_ _"
print "\n"
elif lengthrandomword==3:
print "_ _ _"
print "\n"
...
elif lengthrandomword==15:
print "_ _ _ _ _ _ _ _ _ _ _ _ _ _ _"
print "\n"
else:
print "I thought of a word and it's reeeaaaaaly long, you would never guess it, so I'm just gonna tell you that my word was " + randomword
which you can just shorten to:
if lengthrandomword in range(2,16):
print(' '.join('_' * lengthrandomword))
print('\n')
else:
print("I thought of a word and it's reeeaaaaaly long, you would never guess it, so I'm just gonna tell you that my word was " + randomword)
Upvotes: 1
Reputation: 16733
You need to use some data structures like (say dictionaries) and some loops. This is not how you are supposed to program in any language, the very least in languages like python or ruby.
Upvotes: 0
Reputation: 7582
Use a list comprehension.
short_words = [word for word in txt.splitlines() if len(word) <= 5]
Upvotes: 1
Reputation: 18940
WORDS = txt.splitlines()
WORDS_5_or_less = list(filter(lambda x: len(x) <= 5, WORDS))
then just pick from WORDS_5_or_less
instead of WORDS
Upvotes: 4