I have a hangman type game which displays the secret string as dashes.
So in this case, the secret string is ‘+*794242’, which will first be displayed as ‘——–‘. Now, the takeguess function takes the guessed number and replaces the correct corresponding dash with it. This works for the initial guess, but any gueses afterwards makes the partial variable longer (partial displays the guesses and dashes, ex: ‘–7-4-4-‘).
I began by splitting it to see if it’s the initial guess where partial is blank. I’m not sure why it keeps making the partial string longer after each guess. You’ll see what I mean! Also, if there’s any other better way to do this, I’d like to know after I solve this problem! Thank you!
secret = '+*794242'
partial = ''
def takeguess(a):
incomp = ('-' * len(secret)) #Dashes
if partial == '': # If just starting (first guess), partial is blank
for i in range(0,len(secret)):
if guess == secret[i]:
global partial
partial = partial + guess
else:
global partial
partial = partial + incomp[i]
return partial
else: # After initial guess, partial won't be blank, this is where I need help
for i in range(0,len(secret)):
if guess == secret[i]:
global partial
partial = partial + guess
else:
global partial
partial = partial + incomp[i]
return partial
while True: #Example while true
guess = raw_input('Enter guess: ')
takeguess(guess)
print partial
If you use a set, you could simplify even more.
Tips: