Python Forum
Simple Hangman Game Issue - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Simple Hangman Game Issue (/thread-9332.html)



Simple Hangman Game Issue - andrew95 - Apr-02-2018

Hi, I was wondering if anyone could help me with the output of this simple hangman game? It's returning different values to what I would expect. I believe the issue is where a letter is found in the string, under the else in the second if statement. Any help would be great! Thanks :D Andrew #Issue = The variable word increases without expectation?(Using Python 3)

import random
welcome = input("Welcome to hangman, are you ready to play?").strip()

words = ["eggs"]
no_attempts = 10 

if welcome == "yes" or "Yes" or "y" or "Y" or "okay":
        print(("That's great, let's get started! You have {} tries to find the word!").format(no_attempts))

        word_is = random.choice(words)
        length_of_word = int(len(word_is))
        word = ("."*length_of_word)
        print(word)

        while no_attempts > 0:
            if "." in word:
                first_letter = (input("Choose a letter").lower())
                position_of_letter = int(word_is.find(first_letter))
                if position_of_letter == -1:
                    no_attempts = no_attempts - 1
                    print ("That letter is not in the word, try again!")
                    print(word)
                    guess = input("Would you like to guess the full word itself?").strip().lower()
                    if guess == "no":
                             continue
                    elif guess != word_is:
                             no_attempts = no_attempts - 1
                             print("Wrong! Try guessing another letter")
                             continue
                    elif guess == word_is:
                             well_done = ("Well Done! You have the word!").format(word_is)
                             print(well_done)
                             break
               else:
                    no_attempts = no_attempts - 1
                    print("That's great, that letter is in the word!")
                    where_inside_list = []
                    position = 0                                               
                    while position < len(word_is):                              
                          position = word_is.find(first_letter, position)
                          if position == -1:                                      
                            break
                          where_inside_list.append(position)
                          position += 1   #position = position + 1                                         
                                                                                
                    answer_list = list(word)
                    no_items_list = int(len(where_inside_list))
                    print(where_inside_list)
                    print(answer_list)
                    print(word)
                    target = ([first_letter]*no_items_list)
                    for x,y in zip(where_inside_list, target):
                          answer_list[x] = y

                          word = " ".join(answer_list)
                          print(word)
      
                    #word = word[:position_of_letter] + first_letter + word[position_of_letter + 1:]
                    #print(word)
                    guess = input("Would you like to guess the word?").strip().lower()
                    if guess == "no":
                             continue
                    elif guess != word_is:
                             no_attempts = no_attempts - 1
                             print("Wrong! Try guessing another letter")
                             continue
                    elif guess == word_is:
                             print("Well Done! You have the word!")
                             break
            elif "." not in word:
                    congrats = ("Congratulations, well done! The word is {}").format(word)
                    print(congrats)
                    break
        else:
            no_goes = ("Oh, no! It seems you are out of goes. The word was {}").format(word_is)
            print(no_goes)
elif welcome != "yes" or "y" or "okay":
        print("Oh no, okay, well see you next time!")



RE: Simple Hangman Game Issue - buran - Apr-02-2018

This not takes care of your program problem, but you need to read this and change your line#7
https://python-forum.io/Thread-Multiple-expressions-with-or-keyword


RE: Simple Hangman Game Issue - andrew95 - Apr-02-2018

Hi Buran,

Thanks for your reply, that makes a lot of sense and is an easy error to make!

Thanks!

Andrew