Hello,
I’ve written a basic Hangman game but while I was debugging I discovered that there was a bug, though I have not been able to rectify it after attempting to resolve the issue after modifying the code a few times, and by writing print statements in a number of places:
I’m wondering if it’s appropriate for me to paste the code here, so that I may get a second set of eyes on it. I would really appreciate the help.
The issue is as follows: In my game I prompt the user to enter letters. After I check to see whether they’ve actually entered a singular alphabetic character, I check to see whether this letter had been previously entered as to not penalize /reward the user for a previously entered guess. However, if I repeatedly enter the same incorrect letter several times, and then enter a correct letter, and then continue entering the same incorrect letter several times, followed by another correct letter, i find that the game penalizes me multiple times, even though the point of the safeguards is to only penalize the player only once for each incorrect response.
I think the problem is in the guessCheck function, though that woulnd’t make sense, because the “errors” are not defined in the guessCheck section. Perhaps, at times an incorrect value is still passed on even though they should have already been dealt with in the guessCheck function. Also, there is a words.txt list that I have, but I’m not sure how to upload it
Thank you in advance
[code]
import random
import re
guessTotal = []
def is_alpha(word):
try:
return word.encode('ascii').isalpha()
except:
return False
def ownList():
f = open('words.txt', 'r')
lineList = [line.rstrip('\n') for line in f]
f.close()
#print(count)
length = len(lineList)
num = random.randint(0, length)
word = lineList[num].casefold()
#print(word)
game(word)
#ownList()
def ownWord():
word = input("Please enter your desired word: \n> ")
if(is_alpha(word)):
pass
else:
print("Please insert a valid string")
ownWord()
while(True):
word = str(word).lower()
response = str(input("Would you like to add this word to the list? (enter Y or N) \n> "))
if (response.casefold() == "y"):
f = open("words.txt", "a")
f.write(word)
f.close()
print("Let the game begin! \n")
game(word)
elif (response.casefold() == "n"):
print("Let the game begin! \n")
game(word)
else:
print("Only enter 'Y', or 'N'")
def game(word):
wordLen = len(word)
print(f"The word you are attempting to", \
f"guess is {wordLen} characters long")
guessList = ['_'] * wordLen
error = 0
leftToGuess = wordLen
while(error< 8):
#print(">>>>>> debug 07")
s = guessLetter()
guessTotal.append(s)
#print(">>>>>> debug 08")
guessCor = 0
for i in range(wordLen):
# print(">>>>>> debug 09")
if(s == word[i]):
# print(">>>>>>> debug 10")
guessList[i] = word[i]
guessCor += 1
leftToGuess -= 1
print(f"leftToGuess: {leftToGuess}")
else:
pass
if guessCor > 0:
print(f"\nGood Guess! {s.upper()} was in the word")
print("You have {0} guesses left\n".format((8-error)))
print(guessList)
if leftToGuess == 0:
endGood(word)
else:
error += 1
#guessIncor.append([s])
print(f"WRONG! {s} was NOT in the word")
print("You now have {0} guesses left\n".format(8-error))
print(guessList)
if(error >= 8):
endBad(word)
def endBad(word):
print(f"\n\nYOU LOST!\n The word was {(word.upper())}")
exit(0)
def endGood(word):
print("\n\nCONGRATULATIONS! YOU WON! \n", \
f"The word was {(word.upper())}")
exit(0)
def guessLetter():
s = input("Please enter a letter:\n > ")
if (len(s) > 1):
print("You have entered too many letters")
guessLetter()
#elif not re.match("^[a-z]*$", s):
elif not is_alpha(s):
print("You may only enter letters\n")
guessLetter()
elif (len(s) < 1):
print("You must enter at least one letter")
guessLetter()
elif guessCheck(s.lower()):
#print(">>>>>> debug 05")
return (s.lower())
else:
#print(">>>>>> debug 06")
guessLetter()
def guessCheck(s):
print(guessTotal)
# print(">>>> def guessCheck")
lenTotal = len(guessTotal)
if lenTotal < 1:
# print(">>>>>>>> debug 01")
return True
else:
# print(guessTotal)
# print(">>>>> debug 02")
for i in range(lenTotal):
print("\n")
# print(">>>>>> debug 03")
if (s == str(guessTotal[i])):
print(f"Letter {s} has already been guessed")
guessLetter()
# print(">>>>>> debug 04")
return True
def start():
print("\nWelcome to the Text Based Hangman Game!",\
"\n you have two (2) options: \n", \
"1. Enter your own word \n", \
"2. Have the program select a word for you! \n")
try:
inp = int(input("Please enter 1, or 2 \n > "))
except ValueError:
print("\n\n\n\nPlease enter either 1 or 2\n")
start()
if inp == 1:
ownWord()
elif inp == 2:
ownList()
else:
print("\n\nPlease enter either 1 or 2")
start()
start()
[/code]