-
Notifications
You must be signed in to change notification settings - Fork 46
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remove extra whitespace below this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
An unnecessary empty line.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
An unnecessary empty line.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To make the code more readable, I suggest taking the words from a file.
Here is a code for it:
with open('words.txt','r') as f: text = f.read() words = text.split(' ')
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This line can have the new line command instead of a new print statement.
print("Hangman Game!\n")
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can be removed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use of f string
print(f"Guess the {numberOfLetters} letter word")
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
All of this code can be inserted into if __name__ == '__main__'
like this:
if __name__ == '__main__': choice() guessCount = 10 # Counter variable.We are giving user 10 chances. while True: letterFound = False # Boolean Variable for checking if letter is present. letterEntered = input("Guess any letter: ") # User input indexes = list(find_all(randomWord, letterEntered)) # returns list of index position of a repeated character for index in indexes: """ This statement below puts the guessed letter at right position""" guessWord = guessWord[0:index] + letterEntered + guessWord[index + 1:] letterFound = True if letterFound is False: # Entered letter is not present in the random word guessCount -= 1 # Reduce guessCount by 1 print("Guesses Left: ", guessCount) if guessWord == randomWord: print(randomWord) print("You win!!!") break elif guessCount <= 0: print("You Lose!!!, The word was",randomWord) break print(guessWord)
Added the hangman python code