0

This code came in a file and I didn't change anything about it except the indentation and I did that using PyCharm (not manually). I checked this code many times and everything seems to be fine indentation wise.

import random
    
    
def askForGuess(guessedLetters):
    '''
    CHECKING INPUT
    When the player is guessing, we want to only read letters, so we need to check
    if it's valid input. We can use

                            "isalpha():"

    to check for letters.

    We also need to check if the input was greater than 1 letter. If it's greater
    than 1, it's invallid input. In addition, if the player already guessed a letter,
    that is also improper input
    '''

    validGuess = False
    guess = ""
    while (not validGuess):

        guess = input("Enter a letter to guess")

        if (not guess.isalpha()):  # input has invalid characters in it (not just letters)
            print("Invalid input. Please enter only letters")
        elif (len(guess) > 1):  # guess is not just a single letter
            print("Invalid input. Please enter only a single letter")
        elif (guess in guessedLetters):  # they have already guessed that letter
            print("Invalid input. You have already guessed that letter")
        else:
            validGuess = True

    guessedLetters.append(guess)
    return guess


def updateGuessedWord(guess, word_guessed, chosen_word):
    '''
    Once we have valid input, we need to compare the input to the chosen word.
    We'll use a loop to compare the input to the chosen word, which will replace
    the '-' characters with letters if the letter belongs in that position.

    '''

    for i in range(0, len(chosenWord)):
        if (chosenWord[i] == guess):  # letter @ index i is the letter that was guessed
            wordGuessed[i] = guess
    pass


def gameOver(wordGuessed, chosenWord):
    '''
    Finally we'll check if the player has won or not. If the word_guessed variable
    does not contain anymore '-' characters, the player has won the game. If there's
    still '-' characters, the player hasn't won yet, and we need to ask them to guess 
    again. 
    '''

    if ("-" not in wordGuessed):  # correctly guessed all the letters
        print("Congratulations you win!")
    else:
        print("You lose. The word was : " + chosenWord)

    choice = input("Play again? y/n")

    if (choice == "n" or choice == "no"):
        return False

    pass


def playHangman():
    HANGMAN = (

        """
    -----
    |   |
    |
    |
    |
    |
    |
    |
    |
    --------
    """,
        """
    -----
    |   |
    |   0
    |
    |
    |
    |
    |
    |
    --------
    """,
        """
    -----
    |   |
    |   0
    |  -+-
    |
    |
    |
    |
    |
    --------
    """,
        """
    -----
    |   |
    |   0
    | /-+-
    |
    |
    |
    |
    |
    --------
    """,
        """
    -----
    |   |
    |   0
    | /-+-\ 
    |
    |
    |
    |
    |
    --------
    """,
        """
    -----
    |   |
    |   0
    | /-+-\ 
    |   | 
    |
    |
    |
    |
    --------
    """,
        """
    -----
    |   |
    |   0
    | /-+-\ 
    |   | 
    |   | 
    |
    |
    |
    --------
    """,
        """
    -----
    |   |
    |   0
    | /-+-\ 
    |   | 
    |   | 
    |  |
    |
    |
    --------
    """,
        """
    -----
    |   |
    |   0
    | /-+-\ 
    |   | 
    |   | 
    |  | 
    |  | 
    |
    --------
    """,
        """
    -----
    |   |
    |   0
    | /-+-\ 
    |   | 
    |   | 
    |  | | 
    |  | 
    |
    --------
    """,
        """
    -----
    |   |
    |   0
    | /-+-\ 
    |   | 
    |   | 
    |  | | 
    |  | | 
    |
    --------
    """

    )
    welcome = ['Welcome to Hangman! A word will be chosen at random and',
               'you must try to guess the word correctly letter by letter',
               'before you run out of attempts. Good luck!\n'
               ]

    for line in welcome:
        print(line, sep='\n')

    input("Press 'Enter' to begin!")

    play_again = True

    while play_again:
        print("\n\n\n\n\n")
        print("+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++")
        print("NEW GAME")
        print("+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++")

        '''
        We will create a list of words in a list. Whenever the player wants to 
        play, we will randomly choose a word from that list for the player to guess. 
        What other information do we need to store? The letter our player guesses every 
        turn? Letters the player has chosen already?

        Another thing we need to create is the output for the player to see.
        If the word is 'tiger', we need to display '-----' and fill in the
        letters that our player guesses. If they guess 'i' and 'r', then the 
        output is '-i--r'. We will update this everytime they take a guess
                '''

        words = ["hangman", "chairs", "backpack", "bodywash", "clothing",
                 "computer", "python", "program", "glasses", "sweatshirt",
                 "sweatpants", "mattress", "friends", "clocks", "biology",
                 "algebra", "suitcase", "knives", "ninjas", "shampoo"
                 ]

        chosen_word = random.choice(words).lower()

        guessed_letters = []  # a list of letters guessed so far.

        word_guessed = []  # the progess that the player has made through the word (mix of letters and dashes).

        for letter in chosen_word:
            word_guessed.append(
                "-")  # create an "unguessed" version of the word, number of dashes correspond to number of letters.

        joined_word = None  # joins the words in the list word_guessed as a string for use when printing out progress.

        print(HANGMAN[0])  # print out the initial state of the Hangman.
        attempts = len(HANGMAN) - 1  # determine how many attempts the player will have to guess the word.

        '''
        We'll begin the gameplay down here
        We need to create a loop to check 2 things:
                1) if there are more than 0 attempts
                2) if there are still any '-' characters in our word_guessed variable

        Inside the loop, we need to print out the current hangman and tell the player
        how many more attempts they have before failing
        '''

        while (attempts != 0 and "-" in word_guessed):
            print("\nYou have %i attempts remaining" % attempts)
            joined_word = "".join(word_guessed)
            print(joined_word)

            guess = askForGuess(guessed_letters)

            if (guess in chosen_word):
                print("Correct!")
                updateGuessedWord(guess, word_guessed, chosen_word)
            else:
                attempts -= 1
                print("Wrong! You lost 1 attempt.")

            print(HANGMAN[(len(HANGMAN) - 1) - attempts])

        play_again = gameOver(word_guessed, chosen_word)

    pass

Output is like this

Process finished with exit code 0

I'm still pretty new to Python, and I'm pretty bad at running diagnostics. I also can't figure out where to control the debugging step-by-step on PyCharm.

user3840170
  • 26,597
  • 4
  • 30
  • 62
gwynb2001
  • 11
  • 3
  • 1
    Please format your question so that all code appears as code. – tromgy Jan 15 '22 at 19:52
  • Does this answer your question? [Why is my python function not being executed?](https://stackoverflow.com/questions/29652264/why-is-my-python-function-not-being-executed) – Gino Mempin Jan 16 '22 at 01:11

3 Answers3

0

You are not calling any of the functions in the main.

You should have an invocation of your desired entry point (the name of the function and parameters without indentation)

Alik.Koldobsky
  • 334
  • 1
  • 10
0

playHangman is not called (playHangman()) so nothing is started

InhirCode
  • 338
  • 1
  • 4
  • 5
0

Add a playHangman() on the the end of the file or the standard python main:

if __name__ == '__main__':
    playHangman()
Kevin
  • 785
  • 2
  • 10
  • 32