Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Simple Hangman Game Issue
#1
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!")
Reply
#2
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-...or-keyword
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
Hi Buran,

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

Thanks!

Andrew
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Python Hangman Game - Multiple Letters Problem t0rn 4 4,537 Jun-05-2020, 11:27 AM
Last Post: t0rn
  Simple cards game blackpanda 3 4,184 Apr-10-2020, 08:46 PM
Last Post: TomToad
  Simple Read File Issue blackjesus24 4 2,688 Feb-09-2020, 12:07 AM
Last Post: blackjesus24
  Hangman metro17 4 2,975 Sep-18-2019, 10:59 AM
Last Post: perfringo
  Trouble coding hangman Tay 1 2,331 Mar-28-2019, 01:57 AM
Last Post: ichabod801
  Hangman code problem KrakowKid 1 2,372 Feb-25-2019, 06:29 PM
Last Post: ichabod801
  Python hangman help A1395 11 6,977 Feb-13-2019, 04:24 PM
Last Post: ichabod801
  Need help with simple guessing game! ghost0fkarma 2 2,766 Nov-03-2018, 01:19 AM
Last Post: ghost0fkarma
  Hangman 2skywalkers 3 60,815 Oct-19-2018, 01:49 PM
Last Post: ichabod801
  Easy equipment system for simple game and problems with it naisyrk 3 3,255 Sep-01-2018, 10:05 AM
Last Post: Gribouillis

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020