我有一个关于这个我的hang子手程序的问题。当我猜测一个单词(“好”)有重复的字母('o')右侧时,它只会被显示为第一个索引因为list.index(value)只会返回一个索引,即使该值存在重复。如果我想要同时显示所有重复的字母,我需要更改或添加什么。Hang子手 - 重复的字符
这就是我所期望的程序来做:
猜字母:L >>> _oo_
感谢。
the_word="good"
#print the_word
wLen=len(the_word)
u='_'*wLen
counter=0
while counter!=12 and wLen!=0:
counter=counter+1
print u
g=raw_input("Guess the letter: ")
p=list(the_word)
x1=the_word.find(g)
if x1 >=0:
u1=list(u)
u1[x1]=g
u=''.join(u1)
wLen=wLen-1
if wLen==0:
print "Congratulation!!!you have guessed the word"
print "The word was",the_word
else:
print "sorry u loose"
您是否熟悉递归?这似乎是一个递归函数的完美应用。 while循环也会起作用。 –