#๐Ÿ”’ Hangman strike system not working

7 messages ยท Page 1 of 1 (latest)

tranquil dagger
#

So, I have everything working aside from the strike system, whenever I input a letter it still counts it as a strike but doesnt if the letter isnt in the word instead of the other way around. I dont know if I over did the code or made it too complex for what it is

I've tried redoing the part of the function that sends out the strikes but nothing seems to work

honest robinBOT
#

@tranquil dagger

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

tranquil dagger
#
import random as r
import os
import time

def clr():
    os.system("clear")

# Variables
Word: list = []
Guessed: list = []

playing = True

Running: bool = True
Strikes = 0
Attempts: int = 6

# Choose the word

while playing:

    RandomWords: list = ["kratos", "jax", "apple", "link", "python"]
    Choosing = True
    
    while Choosing:
        WordType: str = input("Would you like to select the word yourself or have one selected randomly? (1: Select Yourself, 2: Select by System)\n[???] ")
        
        clr()
        
        if WordType == "1":
            ChosenWord: str = input("What word would you like to have Player 2 guess?: ").lower()
            Choosing = False
            
        elif WordType == "2":
            Choice: int = r.randint(0, len(RandomWords) - 1)
            
            ChosenWord = RandomWords[Choice]
            Choosing = False
        
        else: print("Please enter a valid option")
            
    for letters in ChosenWord:
        Word.append(letters)
        Guessed.append("_")
    
    # Get player 2's guessed word
    clr()
    
    def CheckGuess(guess, word):
        for i, letter in enumerate(word):
            if letter == guess:
                Guessed[i] = letter
                word[i] = "_"
            if guess not in word:
                global Strikes
                Strikes += 1
                return Strikes
                
            
    
    while (Strikes != 6 and "_" in Guessed):
        print(f"You have {Strikes} strikes")
        print(Guessed)
        Guess: str = input("What letter would you like to guess?\n[???] ").lower()
        
        if not Guess.isalpha():
            clr()
            print("Please use a letter")
        elif len(Guess) > 1:
            clr()
            print("Oops! please use one(1) letter")
        elif Guess in Guessed:
            clr()
            print("Letter has already been guessed, please use another letter")
        else:
#
# Check if player's guessed word is in the string
            CheckGuess(Guess, Word)
            clr()
    
    
    # If its not in the string, add a strike hangman has 6 strikes
    
    if Strikes >= 6:
        print("Im sorry, you have failed")
    elif "_" not in Guessed:
        print("You win!")
        time.sleep(2.5)
    
    clr()
    play = str(input("Play Again? (Y/N):\n[???] ")).lower()
    if play != "y":
        playing = False
    else:
        clr()
potent rune
#

Typically capitalized names communicate that it's a class. Also it's nicer to use snake_case. Your use of both Guess and guess is confusing. Defining a function inside a loop body is kind of weird - you should move it up near your other function declaration.

honest robinBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.