I have coded a game of Hangman. For unknown letters, it prints a _
, but because it is in a while loop it prints a different line. Is there a way to put all the dashes on the same line, so it easier to read?
My code is below:
import time
import random
# Welcoming the user
name = input("What is your name? ")
print("Hello, " + name, "Time to play hangman!")
print(" ")
# Wait for 1 second
time.sleep(1)
print("Start guessing letters...")
time.sleep(0.5)
# Here we set the secret
words = ("pyhton","coding","recycle","dawn","vision","talkative","love","difficulty","late","bake","compact","old","employee","be","bubble","guess","home","urgency","buttocks","shop","fluctuation",
"snack","fling","structure","incentive","enemy","fastidious","overcharge","disagreement","consciousness")
word = random.choice(words)
# Creates an variable with an empty value
guesses = ''
# Determine the number of turns
turns = 10
# Create a while loop
# Check if the turns are more than zero
while turns > 0:
# Make a counter that starts with zero
failed = 0
# For every character in secret word
for char in word:
# See if the character is in the players guess
if char in guesses:
# Print then out the character
print(char)
else:
# If not found, print a dash
print("_")
# And increase the failed counter with one
failed += 1
# If failed is equal to zero
# Print "You Won"
if failed == 0:
print("You won, The word was", word)
# Exit the script
break
print
# Ask the user go guess a character
guess = input("guess a character: ")
# Set the players guess to guesses
guesses += guess
# If the guess is not found in the secret word
if guess not in word:
# Turns counter decreases with 1 (now 9)
turns -= 1
# Print "Wrong"
print("Wrong")
# How many turns are left
print("You have", + turns, 'more guesses' )
# If the turns are equal to zero
if turns == 0:
# Print "You Lose"
print("You Lose")
print("The word was",word)"