s.caplen s.caplen - 16 days ago 6
Python Question

Python Hangman game- why characters are not replaced with the correct one, but displayed with 3 '>'

#this welcomes the user to the game
print ("Hello and welcome to hangman")
#this slows the program down and allows the user to read the first sentence
import time
time.sleep(2)
#this outputs a image of the completed hangman
print ("""
|----
| |
| 0
| -|-
| / |
|
---
""")
time.sleep(2)
#this asks the user for their name
print("what is your name")
name = input()
time.sleep(1)
#this out puts a sentence with the input that the user typed
print("OK,",name, "here comes your random words amount of letters")
time.sleep(2)
import random
#creates a list of names
import time
#this imports the time moduel
names=("variable", "selection","string", "python", "computer", "pseudo code", "output", "algorithm", "hexadecimal", "binary")
import time
time.sleep(2)
#prints random name from list
random = (random.choice(names))
if random == ("varible"):
print ("_ _ _ _ _ _ _ _ 8")
elif random == ("selection"):
print ("_ _ _ _ _ _ _ _ _ 9")
elif random == ("string"):
print ("_ _ _ _ _ _ 6")
elif random == ("python"):
print ("_ _ _ _ _ _ 6")
elif random == ("computer"):
print ("_ _ _ _ _ _ _ _ 8")
elif random == ("pseudo code"):
print ("_ _ _ _ _ _ space _ _ _ _ 6 for first word and 4 for second")
elif random == ("output"):
print ("_ _ _ _ _ _ 6")
elif random == ("algorithm"):
print ("_ _ _ _ _ _ _ _ _ 9")
elif random == ("hexadecimal"):
print ("_ _ _ _ _ _ _ _ _ _ _ 11")
elif random == ("binary"):
print ("_ _ _ _ _ _ 6")
time.sleep(2)
print ("ok,",name, "start guessing")


this is the part that I do not understand and that I can't see why it is not working

this is where the letters are replaced with '_'

guessed = ''
guess = input()
if guess not in random:
print ("""
|
---
""")
lives = 7
while lives < 0:
missed = 0
print()
for guess in random:
print (guess,end=' ')
else:
print ('_',end=' ')
missed = missed + 1
if missed == 0:
print ('You win!')
quit()
break
guess=input("please guess a letter:")

guessed = guessed + guess
if guess not in random:
print ("that is not in the word")
print(""" |
---""")
time.sleep(2)
print ('please try another letter.')
lives = lives - 1
missed = missed + 1

print('your new lives value is')

print(lives)


This is the end of the code I will repeat the above when I can get it working

Answer

This is the program that works. What has been changed is some of the variable names, what some things are equal to and I've added some explanations too, with the #.

#this welcomes the user to the game 
print ("Hello and welcome to hangman")
#this slows the program down and allows the user to read the first sentence 
import time      
time.sleep(2)
#this outputs a image of the completed hangman
print ("""
        |----
        |    |
        |    0
        |   -|-
        |   / |
        |     
       ---
""")
time.sleep(2)
#this asks the user for their name 
print("what is your name")
name = input()
time.sleep(1)
#this out puts a sentence with the input that the user typed 
time.sleep(2)
import random
#creates a list of names
import time
#this imports the time moduel
print ("Program now generating word")
names=("variable", "selection","string", "python", "computer", "pseudo     code", "output", "algorithm", "hexadecimal", "binary")
import time
time.sleep(2)
#prints random name from list
selected_word = random.choice(names)
guessed = ''

lives = 7

#this section creates the '_' bits, and fills it in when the letter is guessed.
#It also makes it so it keeps asking until they have run out of lives
while lives > 0:

    missed = 0
    print()
    for letter in selected_word:
        if letter in guessed:
            print (letter,end=' ')
        else:
            print ('_',end=' ')
            missed = missed + 1
    if missed == 0:
        print ('You win!')  
        quit()
        break
    guess=input("please guess a letter:")

#this part checks if the guessed letter is in the word
    guessed = guessed + guess
    if guess not in selected_word:

        print ('\n That letter is not in this word, your lives have been decreased by 1.')
        print ('Please try another letter.')
        lives = lives - 1 
        missed = missed + 1

        print('your new lives value is')

        print(lives)
#this is the Askii code for the hangman. If the lives are less than 7, they will appear.
        if lives < 7:
            print('''   _______
   |    |   ''')
            if lives < 6:
                print('   |    O    ') 
            if lives < 5:            
                print('   |    |    ')
            if lives < 4:
                print('   |  \- -/  ')
            if lives < 3: 
                print('   |    |    ')
            if lives < 2:
                print('   |   / \\  ')
            if lives < 1:
                print('   |  /   \\ ')
            if lives == 0:
             print('___|___      ')

             print('GAME OVER! You have run out of lives and lost the game')
             print('The random word was...')
             print(selected_word)
             quit()

You may want to change some of the variable names, if you wish.