Hello everybody
Inputting the same character multiple times doesn’t work.
Here is my code
Please be noted that I’ve unnecessarily complicated my code by working on it for a long time to resolve the problem.
I’d really appreciate a correction from anybody.
Thanks in advance
import random
print("Hangman Game")
name = input("Enter your name:\n")
print("Let's get started " + name)
def choice():
global wordlen
global attempts
tries = 0
while tries<5:
wordlen = input("Enter word length(4-9):")
attempts = input("How many attempts do you need(5-10):")
try:
if 4 > int(wordlen) or int(wordlen) > 9:
tries = tries + 1
else:
pass
tries = 6
except ValueError:
tries = tries + 1
print("Invalid input\n")
if tries == 5:
print("Try again Later \n Bye")
wordpick()
def wordpick():
global wordlen
wordsfile = open("wordlist.txt", "r")
wordlist =[]
for line in wordsfile:
stripped_line = line.strip()
if len(stripped_line) == int(wordlen):
wordlist.append(stripped_line)
global chosenword
chosenword = random.choice(wordlist)
gamefunction()
def gamefunction():
global chosenword
global wordlen
global attempts
chosenwordlist = []
tally = 1
print("\nThe word to guess is....\n")
print(chosenword)
display = "_" * len(chosenword)
print(display)
for i in chosenword:
chosenwordlist.append(i)
print(chosenwordlist)
# print(letter)
while tally <= int(attempts):
userchoice = input()
guessedletters = []
sameletterguess = []
if len(userchoice.strip()) == 1:
occcurence1 = chosenword.find(userchoice)
if (userchoice in chosenword) and (chosenwordlist.count(userchoice) == 1):
display = display[:occcurence1] + userchoice + display[occcurence1 + 1:]
guessedletters.append(userchoice)
elif (userchoice in chosenword) and (chosenwordlist.count(userchoice) > 1):
sameletterguess.append(userchoice)
display = display[:occcurence1] + userchoice + display[occcurence1 + 1:]
guessedletters.append(userchoice)
print(sameletterguess)
print(sameletterguess.count(userchoice))
if sameletterguess.count(userchoice) == 2:
occurence2 = chosenword.find(userchoice,chosenword.find(userchoice)+1)
display = display[:occurence2] + userchoice + display[occurence2 + 1:]
guessedletters.append(userchoice)
print(sameletterguess)
elif sameletterguess.count(userchoice) == 3:
occurence3 = chosenword.find(userchoice, chosenword.find(userchoice,chosenword.find(userchoice)+1)+1)
display = display[:occurence3] + userchoice + display[occurence3 + 1:]
guessedletters.append(userchoice)
print(sameletterguess)
elif sameletterguess.count(userchoice) == 4:
occurence4 = chosenword.find(userchoice, chosenword.find(userchoice, chosenword.find(userchoice,chosenword.find(userchoice)+1)+1)+1)
display = display[:occurence4] + userchoice + display[occurence4 + 1:]
guessedletters.append(userchoice)
print(sameletterguess)
else:
print("Wrong guess")
tally = tally + 1
print("\n" + display)
if display == chosenword:
tally = int(attempts) + 1
print("Congratulations\nYou guessed the word correctly")
else:
print("Invalid input\nEnter only a single letter\n")
print(display)
gamecontinue = False
while gamecontinue == False:
playprompt = input("Do you want to play again?\n Enter 'Y' for yes and 'N' to quit \n")
if playprompt == "y" or playprompt == "Y":
choice()
elif playprompt == "n" or playprompt == "N":
print("Thanks for playing \n Come back Later :)")
gamecontinue = True
else:
print("Invalid Input\n Enter valid input")
choice()