Hi, I'm trying to write a hangman game program, but some things aren't working properly.
def hangman(): choice=input("Enter a letter: ") a=type.find(str(choice)) if len(choice)>1: print ("Only one letter please") hangman() elif a!=-1: b[a]=str(choice) print ("Correct") c=''.join(b) print (c) if type==c: print ("WINNAR!") else: hangman() else: d+=1 e[d]=choice if d==0: print ("O",e) hangman() elif d==1: print (e,"O<") hangman() elif d==2: print (e,"0<-") hangman() elif d==3: print (e,"O<-<\n You lose!") if __name__=='__main__': type=input("Enter a word: ") type=''.join(type.lower().split(' ')) b=['_ ']*len(type) print (''.join(b)) d=-1 e=['']*5 hangman()
After running and testing the program several times I've discovered some problems.
- When more than one of the same letter is in a word, only the first letter is added. For example, if I used the word 'moose', then only the first 'o' would show, and I can't enter another 'o'.
- Capital letters cannot be entered, only when first entering the word.
- I also want a way to randomly generate words instead of the person entering it because it's not hangman if you know the word already.
- When you don't enter a word and just hit enter, the program still runs.
- When you enter a letter that isn't part of the word it gives you an error.
- If I enter a letter, then a space for when it asks me for a letter, then it screws up the program a little (not sure how to describe).
I want to incorporate error handling, and a class, but I'm not completely sure on how to do them.
Thanks for reading.