I am creating a Python3 hangman game and when the game finished I would like to prompt the user for an input which has two outcomes, either he wants to play again or finish. How can I implement that feature into the code, so that it starts the loop again? Is it better to create a loop over the whole game or should I create an if statement?
That's my current code (not finished and finalized):
import random
# from collections import Counter
unknown_words_list = [" "]
known_words_list = [" "]
alphabet = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v",
"w", "x", "y", "z"]
name = input("What is your name? ")
print("Hello, " + name, "lets play hangman. I'll be guessing your word. Don't cheat ;)")
time.sleep(0.5)
print("Think about a word with at least 2 Characters.")
"""
time.sleep(0.5)
user_word = input("Type your word here: ")
x = len(user_word)
"""
user_word = input("Type your word here: ")
x = len(user_word)
turns = x * 2 # amount of guesses for the bot multiply the letters of the users word
guesses = ""
time.sleep(0.5)
bot_guess = "" # letters the bot has guessed correctly
failed = 0 # fail counter
# while loop to avoid a one character game.
while x < 2:
print("Haven't we agreed on a word that has at least 2 characters?"
"Please try again: ")
user_word = input()
x = len(user_word)
if x > 2:
print("Great, managed to find a word with at least 2 characters.")
break
print("No worries, I won't know your word by the way, but at the end of our game I will add it to my Library, "
"if it isn't there yet.")
time.sleep(0.5)
print("Has your word " + str(x), "characters?")
print("For your word I will have " + str(turns), "turns to guess"
"and can fail maximum 10 times.")
while turns > 0 and failed < 10:
random_guess = random.choice(alphabet)
print("My guess is the letter " + random_guess, ".")
alphabet.remove(random_guess)
turns -= 1
time.sleep(0.5)
if random_guess in user_word:
bot_guess += random_guess
print(bot_guess)
else:
failed += 1
if failed == 9:
print("I guessed to often wrong, you win.")
elif turns <= 0:
print("I reached the maximum amount of turns and could not figure out the word, you win.")
# adding not guessed words to unknown list
def unidentified_word():
time.sleep(0.5)
print("Hmm, I was not able to guess your word in time, this should teach me a lesson!")
if user_word not in unknown_words_list:
print("I'll add this word to my collection of unknown words which I also did not guess.")
unknown_words_list.append(user_word)
return
# adding not guessed words to known list
def identified_word():
time.sleep(0.5)
print("Yeey, I was able to guess your word after " + str(guesses), " guesses. ")
if user_word not in known_words_list:
time.sleep(0.5)
print("I'll add this word to my collection of correctly guessed words.")
known_words_list.append(user_word)
return