I have coded this hangman game to test something, and it doesn't work properly.
Here is the code:
import random
from string import ascii_lowercase, ascii_uppercase
secret_words = ["screen", "secret", "game", "hangman"]
def lowerChar(c):
if 'A' <= c <= 'Z':
return ascii_lowercase[ascii_uppercase.index(c)]
else:
return c
def get_guess():
word = random.choice(secret_words)
length = len(word)
limit = 5
count = 0
display = "*" * length
print(display, " Is the word")
while count < 5:
letter = input("Please Enter Your guess letter")
for l in word:
if letter.lower() == lowerChar(l):
print("Right guess!")
index = word.find(l)
display = display[:index] + letter + display[index + 1: ]
print(display)
if not '*' in display:
print("You won!")
break
else:
count += 1
print("You got it incorrect, you have: ", limit - count , " Additional trials")
get_guess()
You are doing too much inside the block for l in word
. You have to check, whether the character is contained in the word or not, outside this loop:
import random
secret_words = ["screen", "secret", "game", "hangman"]
def get_guess():
word = random.choice(secret_words)
count = 0
display = "*" * len(word)
print(display, " Is the word")
while count < 5:
letter = input("Please Enter Your guess letter")
if letter.lower() not in word:
count += 1
print("You got it incorrect, you have: ", limit - count , " Additional trials")
else:
print("Right guess!")
new_display = ''
for w, l in zip(word, display):
if letter.lower() == w:
new_display += letter.lower()
else:
new_display += l
display = new_display
print(display)
if not '*' in display:
print("You won!")
break
get_guess()
An alternative solution to Daniel's would be introducing a variable called goodGuess
that allows you to check if the person has made a correct guess in the for loop, and then once you have exited the for loop, then checking if the player has made an incorrect guess, see below
import random
from string import ascii_lowercase, ascii_uppercase
secret_words = ["screen", "secret", "game", "hangman"]
def lowerChar(c):
if 'A' <= c <= 'Z':
return ascii_lowercase[ascii_uppercase.index(c)]
else:
return c
def get_guess():
word = random.choice(secret_words)
length = len(word)
limit = 5
count = 0
display = "*" * length
print(display, " Is the word")
while count < 5:
letter = input("Please Enter Your guess letter")
#as we do not yet know if this guess is good we set it to False until we do know
goodGuess = False
for l in word:
if letter.lower() == lowerChar(l):
#as the player has made a correct guess we set goodGuess to True
goodGuess = True
print("Right guess!")
index = word.find(l)
display = display[:index] + letter + display[index + 1: ]
print(display)
if not '*' in display:
print("You won!")
break
#checking if the player has made a correct guess
if not goodGuess:
count += 1
print("You got it incorrect, you have: ", limit - count , " Additional trials")
get_guess()
Hope this helps.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.