Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 Simple Hangman Game Issue
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)

        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!")
                    guess = input("Would you like to guess the full word itself?").strip().lower()
                    if guess == "no":
                    elif guess != word_is:
                             no_attempts = no_attempts - 1
                             print("Wrong! Try guessing another letter")
                    elif guess == word_is:
                             well_done = ("Well Done! You have the word!").format(word_is)
                    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:                                      
                          position += 1   #position = position + 1                                         
                    answer_list = list(word)
                    no_items_list = int(len(where_inside_list))
                    target = ([first_letter]*no_items_list)
                    for x,y in zip(where_inside_list, target):
                          answer_list[x] = y

                          word = " ".join(answer_list)
                    #word = word[:position_of_letter] + first_letter + word[position_of_letter + 1:]
                    guess = input("Would you like to guess the word?").strip().lower()
                    if guess == "no":
                    elif guess != word_is:
                             no_attempts = no_attempts - 1
                             print("Wrong! Try guessing another letter")
                    elif guess == word_is:
                             print("Well Done! You have the word!")
            elif "." not in word:
                    congrats = ("Congratulations, well done! The word is {}").format(word)
            no_goes = ("Oh, no! It seems you are out of goes. The word was {}").format(word_is)
elif welcome != "yes" or "y" or "okay":
        print("Oh no, okay, well see you next time!")
buran wrote Apr-02-2018, 11:44 AM:
Don't try to add color to your code - it makes impossible to run it
This not takes care of your program problem, but you need to read this and change your line#7
Hi Buran,

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



Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Trouble coding hangman Tay 1 261 Mar-28-2019, 01:57 AM
Last Post: ichabod801
  Made a simple script for android (really simple) but it is not running anddontyoucomebacknomore 2 276 Mar-06-2019, 12:19 AM
Last Post: anddontyoucomebacknomore
  Hangman code problem KrakowKid 1 255 Feb-25-2019, 06:29 PM
Last Post: ichabod801
  Python hangman help A1395 11 540 Feb-13-2019, 04:24 PM
Last Post: ichabod801
  Need help with simple guessing game! ghost0fkarma 2 360 Nov-03-2018, 01:19 AM
Last Post: ghost0fkarma
  Hangman 2skywalkers 3 823 Oct-19-2018, 01:49 PM
Last Post: ichabod801
  Easy equipment system for simple game and problems with it naisyrk 3 513 Sep-01-2018, 10:05 AM
Last Post: Gribouillis
  Hangman Help. 2skywalkers 4 900 Jun-26-2018, 02:49 AM
Last Post: ichabod801
  Python Hangman Replacing "_" with letters. 2skywalkers 6 2,137 Jun-25-2018, 12:01 PM
Last Post: gruntfutuk
  Trying to write a simple (but not so simple for me) trading algo lightbot 1 665 Jun-15-2018, 09:44 AM
Last Post: Larz60+

Forum Jump:

Users browsing this thread: 1 Guest(s)