Hangman Game in Python 3.x


#1

#2

i would use end, like i did here:

hangman = [
"""
-------o""",
"""
       ^
       |
       |
       |
       |
       |
-------o""",
"""
  -----^
       |
       |
       |
       |
       |
-------o""",
"""
  -----^
  |    |
       |
       |
       |
       |
-------o""",
"""
  -----^
  |    |
  O    |
       |
       |
       |
-------o""",
"""
  -----^
  |    |
  O    |
  |    |
       |
       |
-------+""",
"""
  -----^
  |    |
  O    |
 /|    |
       |
       |
-------o """,
"""
  -----^
  |    |
  O    |
 /|\   |
       |
       |
-------+""",
"""
  -----^
  |    |
  O    |
 /|\   |
 /     |
       |
-------o""",
"""
  -----^
  |    |
 *_*   |
 /|\   |
 / \   |
       |
-------o"""]

name = input("\n\nHello there what's your name? ")

print("\nNice to meet you " + name)

ask = input("\nWanna play a 2 player game of Hangman? ").upper()

if ask == 'YES' or ask == 'Y':
    print("Let's get started then!\n\n") 
else:
    exit(0)

print("""\nHow this works is Player 1
gives Player 2 a word to guess,
or vice versa.\n
This is a simple hangman so your word cannot have two of the same letters.""")

tries = len(hangman) - 1
hz = 0 #size_of_hangman
word = input("\nEnter your hidden word: ")
wrong = 0

#making sure the user doesn't know the word they are to find
for n in range(50): 
    print(" ")


guessed = " " #letters guessed
correct = ""
print(hangman[hz])

while len(correct) != len(word):
    print('\n Bob: ' + hangman[hz])
    print()
    for char in word:
        if char in guessed:
            print(char, end=" ")
        else:
            print("_", end=" ")
            wrong += 1
    if wrong == 0:
        print()
        print("\nYOU WON!\n"*50)
        print()
        break
    #make the user keep guessing
    print("\nPress [ Enter ] when they are no more chars remaining!\n")
    guess = input("\nEnter a letter you think is in the word: ")
    guessed += guess #guessed = guessed + guess
    if guess not in word:
        print()
        print("Sorry, [{:s}] is not in my word.".format(guess))
        print()
        hz+=1
        tries -= 1
        print("\nYou have " + str(tries) + " turns left.")
    else:
        if guess in word:
                correct += char #correct = correct + guess
    if tries == 0:
        print("\nYou Lose.")
        print()
        print("\n Sadly your friend has been hanged!")
        print('\nHangman: ' + hangman[hz])
if len(correct) == len(word):
    print("\nYOU WON!\n"*50)
    print()
    print("\nThe word was, " + word + "\n")
    exit(0)

this will print the under scores and guessed letters on the same line, its the equivalant of python2 ,:

print "on the",
print "same line"

#3

Noted and Changed! Always a pleasure @stetim94


#4

i don't know if you saw it, but i also added:

print("\nPress [ Enter ] when they are no more chars remaining!\n")

a new line character at the beginning of this string. otherwise it would be on the same line as the word we are guessing

For the rest, good job :slight_smile:


#5

Actually I didn't but that is a significant change to to the ui of the program,

Thanks, I still remember the first time i pm'ed you, when I couldn't even fix my battleship code, now I'm writing my own code code from scratch :laughing::smirk:


#6

a positive change, no? the \n was needed after adding end

You have hugely improved, keep up the good work :slight_smile:


#7

Quite positive, actually I used end in the beginning but then removed it because of the output,

Much appreciated. :slight_smile: