0
How will i do this one ?
i am doing "the man the hang" but i have a problem . word = input() while True: char = input() for i in range(len(word)) print(char if char==word[i] else "_",end=" ") i need to create memory ? char is going (excuse me , i am learning new english language)
3 Answers
+ 1
'''
Merhaba and Hello,
Maybe this gives you a direction. Try it out!
'''
word = "Hello World".replace(" ", "").upper()
chars = []
for i in range(len(word)):
chars.append(word[i])
wordHidden = ["_"] * len(word)
while True:
char = str(input("Type Character: ")).upper()
if len(char) > 1:
print("Only 1 Character Please")
continue
if char in chars:
for i in range(len(chars)):
if char == chars[i]:
wordHidden[i] = char
print(wordHidden)
if "_" not in wordHidden:
break
print("Congrats!")
+ 1
Something like this what you're after?
# Enter the hangman word at the prompt and change it to upper case
word = input("Enter a word. ").upper()
# Create a dictionary from the word and set all letter values to False
ans = {key: False for key in list(word)}
# Create a boolean for whether or not the game has been won
won = False
# Give the player so many wrong guess
guesses = 5
# Loop until the game is won or player runs out of guesses
while not won and guesses > 0:
# Get a letter from the player and change it to upper case
char = input("Enter a letter. ").upper()
# Check if the letter is in the word
if char in word:
# If the letter is in the word change its dictionary value to True
ans[char] = True
else:
# If the letter isn't in the word subtract 1 from guesses
guesses -= 1
# loop through the dictionary to see if the player has guessed all the letters correctly
for key, value in ans.items():
if value == False:
# if the loop comes across a letter that hasn't been guessed
# make sure won is False
won = False
# and break out of the for loop
break
# if all the letters have been guessed set won to True
won = True
# Loop for the length of letters in the word
for i in range(len(word)):
# Print the letter if it has already been guessed otherwise print an underscore.
print(word[i] if ans[word[i]] else "_", end="")
# print a new line
print()
# When the loop is complete
# Check to see if the player has won or lost
if won and guesses > 0:
print("You Won!")
else:
print("You lost. Try again!")
0
Thank you so much , it works !