Reputation: 1
I created a guess the word game and basically when you run it it will ask you to enter 3 words and you have 10 lives to try and guess it. But I have a some problems, the loop wont end and some sentences keep repeating and I don't know how to save the hidden so it will be saved for the next letter. If someone can help me I will be very thankful.
Here is the code:
mport re
import sys
import random
FirstWord = input("Enter your first word: ")
if not re.match("^[a-z]*$", FirstWord):
print ("Sorry Only non capital letters are allowed.")
sys.exit()
SecondWord = input("Enter your Second word: ")
if not re.match("^[a-z]*$", SecondWord):
print ("Sorry Only letters are allowed.")
sys.exit()
ThirdWord = input("Enter your Third word: ")
if not re.match("^[a-z]*$", ThirdWord):
print ("Sorry Only letters are allowed.")
sys.exit()
Words = [FirstWord,SecondWord,ThirdWord]
try:
fh = open("/Users/naomi/Documents/Vocabulary.txt","a")
except IOError as e:
print("File does not exist or error when opening")
exit()
else:
fh.write (FirstWord)
fh.write (SecondWord)
fh.write (ThirdWord)
fh.close()
GeneratedWord = random.choice(Words)
hidden = '_ ' * len(GeneratedWord)
print ()
print ("Word: ",hidden)
count =10
while count >=1 :
guess = input("Take a guess ")
if len(guess) != 1:
print ("Only one letter can be entered at a time!")
elif guess not in 'abcdefghijklmnopqrstuvwxyz':
print ("Only letters are excepted!")
for letter in GeneratedWord:
if letter in GeneratedWord:
show = GeneratedWord.index(guess)
unhidden = hidden[:show*2] + guess + hidden[show*2+1:]
print ("Guess is correct!")
else:
count = count-1
print ("Your guess was wrong")
print()
print(unhidden)
Upvotes: 0
Views: 174
Reputation: 4528
I think this line has a logical error
for letter in GeneratedWords:
if letter in GeneratedWords:
Change it to:
for letter in GeneratedWords:
if (guess == letter):
Or:
if (guess in GeneratedWords):
Upvotes: 0
Reputation: 1347
It looks like in your second If statement, your counter is only getting decremented if the guess is wrong. I'm assuming you need your counter to decrement every time they make a guess, whether it's right or wrong.
Upvotes: 1