Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- sAnswer = ##( a word imported from a dictionary text file)
- sLetterGuessed = " "
- sLetterUsed = " "
- sReveal = len(sAnswer) * "_ "
- while True:
- sLetterGuessed = raw_input("Enter a letter or guess the word: ").lower()
- while True:
- if sLetterGuessed in sLettersUsed:
- sLettersUsed += sLetterGuessed
- print "can't use that"
- break
- elif sLetterGuessed in sAnswer:
- sReveal = " ".join([x if x== sLetterGuessed else "_" for x in sAnswer ])
- sLettersUsed += sLetterGuessed
- print (sReveal)
- print HangingMan[iErrors]
- break
- elif sLetterGuessed not in sAnswer:
- sReveal = " ".join([x if x== sLetterGuessed else "_" for x in sAnswer ])
- sLettersUsed += sLetterGuessed
- break
- ## Hangman game
- ## A random word for example "pizza" will become "_ _ _ _ _ " Player will guess "z" output will be "_ _ z z _ " The current code does show this, but as it's in a loop it will always reset back to blank underscores. If I guess again, "a" it should be "_ _ z z a " but that's not what's happening it becomes "_ _ _ _ a " instead. Please help me with this ordeal !
- sAnswer = ##( a word imported from a dictionary text file)
- sLetterGuessed = " "
- # string containing all of the letters guessed
- sLetterUsed = " "
- # string containing all of the letters guessed correctly
- sReveal = len(sAnswer) * "_ "
- # ln(sAnswer) will return a number, multiply that number by "_ "
- # ex. 5 * "_ " -> "_ _ _ _ _ "
- while True:
- sLetterGuessed = raw_input("Enter a letter or guess the word: ").lower()
- # initialize the letter the user is guessing and convert it to lower case
- # raw input will get the user's input
- while True:
- if sLetterGuessed in sLettersUsed:
- # is the letter guessed a letter they have already guessed?
- # the code checks to see if sLetterGuessed is in the string sLettersUsed
- sLettersUsed += sLetterGuessed
- # add that later to the letters they have guessed
- # i.e. if the user guessed "s" and the letters they have guessed were "abc" the
- # new sLettersUsed will be "abcs"
- print "can't use that"
- elif sLetterGuessed in sAnswer:
- # is the letter the user guessed in the answer word
- # i.e. is "o" in "word"
- sReveal = " ".join([x if x== sLetterGuessed else "_" for x in sAnswer ])
- # join (from the docs) returns a string that is a concatenation of the strings
- # in the iterable.
- # Essentially what this line is doing is replacing the "_" with the correct
- # letter where the correct letter is position. I'm assuming you have played
- # hangman before.
- sLettersUsed += sLetterGuessed
- print (sReveal)
- print HangingMan[iErrors]
- # not sure where HangingMan comes from. the purpose is to print the number of errors."
- elif sLetterGuessed not in sAnswer:
- # if the user makes an incorrect guess.
- sReveal = " ".join([x if x== sLetterGuessed else "_" for x in sAnswer ])
- # same reveal function as above.
- sLettersUsed += sLetterGuessed
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement