Skip to content

Instantly share code, notes, and snippets.

Created May 26, 2014 09:06
Show Gist options
  • Save anonymous/1135f8575f9bec4fa8e2 to your computer and use it in GitHub Desktop.
Save anonymous/1135f8575f9bec4fa8e2 to your computer and use it in GitHub Desktop.
import random
f = open("enable1.txt")
words = [x.strip().upper() for x in f.readlines()]
def CreateWords(difficulty=1):
"""Take the difficulty number and a list of words, return
a list of words accordingly"""
newwords = []
i = 0
temp = ""
while i < 5:
temp = random.choice(words)
if len(temp) == difficulty + 4:
newwords.append(temp)
i += 1
return newwords
def CheckMatch(userinput, answer):
"""Take user input and the answer word, return number of letters user
matched"""
i = 0
count = 0
for x in answer:
if i >= len(userinput):
break
if x == userinput[i]:
count += 1
i += 1
return count
def PrintResult(count, answer):
"""Take number of correct matches and the answer and print the results"""
if count == len(answer):
print "You win!"
else:
print "%d out of %d correct" % (count, len(answer) )
while True:
difficultynum = input("Difficulty (1-5) ? ")
if 1 <= difficultynum <= 5:
break
wordlist = CreateWords(difficultynum)
for x in wordlist:
print x
y = count = 0
answer = random.choice(wordlist)
while y < range(4) and count != len(answer):
userword = input()
count = CheckMatch(userword, answer)
PrintResult(count, answer)
print "%d / 4 tries left" % y
y += 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment