I am trying to make a hangman game and I am not fully sure on how to add a specific letter to a variable. For example, adding a letter that someone chose through the pythons' input prompt to a variable. Here is the code I am working on:
import random
import time
word_list = ['that', 'poop', 'situation', 'coding', 'python', 'turtle', 'random', 'passive', 'neutral', 'factor', 'time']
word_chosen = random.choice(word_list)
your_name = input("What is your name?")
time.sleep(1)
print("Hello " + your_name + ", lets play some hangman!, The amount of letters in the word is below!")
guesses = 7
time.sleep(1)
hidden_word = ""
for i in word_chosen:
hidden_word += "-"
while True:
n = 1
time.sleep(1)
print("Your word is: " + hidden_word)
print("___________")
print("|")
print("|")
print("|")
print("|")
print("|")
print("|")
print("|")
characters = (word_chosen)
time.sleep(1)
letters_correct = ""
characters_guessed = input("Type in the letters you think are in this word!")
for i in characters:
hidden_word == characters_guessed
if characters_guessed == characters:
print(hidden_word + characters)
else:
int(guesses) - int(n)
print("Nope, sorry.")
I have modified your code @Noob and made it work. Instead of using strings I have made lists of chosen_word ,characters and characters_guessed .In this code you can even enter more than one word at a time. It automatically fillsup the repitive words and will tell the user if he or she has won or lost the game.
import random
import time
word_list = ['that', 'poop', 'situation', 'coding', 'python', 'turtle', 'random', 'passive', 'neutral', 'factor', 'time']
word_chosen = random.choice(word_list)
your_name = input("What is your name?")
time.sleep(1)
print("Hello " + your_name + ", lets play some hangman!, The amount of letters in the word is below!")
guesses = 7
time.sleep(1)
hidden_word = [" - " for i in range(len(word_chosen))]
while guesses>0:
time.sleep(1)
print("Your word is: "+ "".join(hidden_word))
print("___________")
for i in range(7):
print("|")
characters = list(word_chosen)
time.sleep(1)
letters_correct = ""
characters_guessed = input("Type in the letters you think are in this word!").split()
for j in characters_guessed:
if j not in characters:
guesses-=1
print(f"Wrong guess, {j} is not in the word")
else:
for i in characters:
if i==j:
find_position=0
for k in range(characters.count(i)):
hidden_word[characters.index(i,find_position)]=j
find_position+=characters.index(i,find_position)+1
if " - " not in hidden_word:
print("Congratulations, you have won the game.The word was {chosen_word}")
break
elif guesses==0:
print("Opps!! out of guesses. You have lost the game.")
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments