Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/315.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
刽子手-查找2个字符python_Python_Indexing - Fatal编程技术网

刽子手-查找2个字符python

刽子手-查找2个字符python,python,indexing,Python,Indexing,我的任务是用Python制作一个刽子手程序。他就是我到目前为止所做的: newList = [] f = open('words.txt','r') for i in range(120): wlist=f.readline() newList.append(wlist) print newList print len(newList) import random a=(random.choice(newList)) c=len(a)-1 print a e='_ ' * c

我的任务是用Python制作一个刽子手程序。他就是我到目前为止所做的:

newList = []
f = open('words.txt','r')
for i in range(120):
    wlist=f.readline()
    newList.append(wlist)
print newList
print len(newList)

import random
a=(random.choice(newList))
c=len(a)-1
print a
e='_ ' * c
print "I'm thinking of the", c, "- letter word:", e
b=raw_input("Make a choice: ")

s=list(e)
if b in a:
    p=a.index(b)
    s[p*2]=b
    print p
    print ''.join(s)
问题是,如果单词有两个或两个以上相同的字母(例如:position),并且用户猜测字母o,那么它将出现o而不是o! 我怎样才能解决这个问题?非常感谢

def findIndexes(s, ch):
    return [i for i, letter in enumerate(s) if letter == ch]

a="position"
print (a)
c=len(a)

e = '_ ' * c
print ("I'm thinking of the", c, "- letter word:", e)
b = raw_input("Make a choice: ")

s=list(e)
if b in a:
    indx = findIndexes(a,b)
    for i in indx:
        s[i*2]=b
        print (i)
    print (''.join(s))
输入输出
o

position
I'm thinking of the 8 - letter word: _ _ _ _ _ _ _ _ 
Make a choice: o
1
6
_ o _ _ _ _ o _ 
输入的输出
n

position
I'm thinking of the 8 - letter word: _ _ _ _ _ _ _ _ 
Make a choice: n
7
_ _ _ _ _ _ _ n
因为我没有你的txt,我自己分配了一个
a

关于代码,我定义了一个函数,该函数使用查找字符串中出现的所有字母,并将它们放入列表中。然后,我使用该列表将这些索引处的下划线切换为输入字母