python 2.7 - Multiple Errors in Hangman program -
i've been teaching self program in python lately , decided make game. have been successful majority of except few things
- when incorrect letter inputted, letter appended wrong letter array many spots in gamespace array userletter equal.example: if type in "the" wordtarget, t, h , e printed 2 times wrong letter array. no matter letter gets out wrong letter array
code:
wordtarget = raw_input("enter word here: ").lower () gamespace = ["_"] * len(wordtarget) wrongletter = [] def main(): x in range(0, len(wordtarget)): while (gamespace[x] != wordtarget[x]): getletter() in range(0,len(wordtarget)): if (wordtarget[i] == userletter): gamespace[i] = userletter elif (wordtarget[i] != userletter): print "letter incorrect, try again" wrongletter.append(userletter) print ("incorrect letters: %s" % wrongletter) print ("correct letters: %s" % gamespace) if (gamespace == wordtarget): print "congratulations! won" main()
i'm guessing problem lies loops i'm running , way i'm checking right answer can't figure out.
the loop checking letter seems issue. try replacing with:
position = 0 ptr = wordtarget.find(userletter, position) while ptr != -1: gamespace[ptr] = userletter position = ptr + 1 ptr = wordtarget.find(userletter, position) if position == 0: # no match ever found print "letter incorrect, try again" wrongletter.append(userletter)
also replace original loop while ("".join(gamespace) != wordtarget):
, , should go.
Comments
Post a Comment