如何查看符号是否匹配?

时间:2015-02-22 16:02:44

标签: python

我正在制作一个代码拼图,其中将向用户提供由符号加密的十个单词的列表。他们必须决定哪些符号由哪些符号表示。我必须这样做,如果某个符号已被用户替换,则相同的符号不能被另一个字母替换。怎么做?

print "Python Puzzle Model\n\n\n"
textfile = open('words.txt')
words = textfile.read()
print words
print '\n\n'
print 'Lets replace the given code' 
words =  words.replace('#','o')


words =  words.replace('!','k')
print words

print '\n'
#print 'Which letter would you like to replace?\n'
x = raw_input(" Which symbol would you like to replace ")
y = raw_input(" Which letter would you like to replace it with ")
print '\n'

words = words.replace(x,y)
print '\n'

print words

1 个答案:

答案 0 :(得分:0)

我认为这样的事情,除非我误解了你的问题:

textfile = open('a.txt')
replace_dict_list = []

replace_symbol =  raw_input(" Which symbol would you like to replace ")
replace_letter = raw_input(" Which letter would you like to replace it with ")

dict = {replace_symbol:replace_letter}

already_exists = False
for item in replace_dict_list:
    if replace_symbol in item:
        already_exists = True
    else:
        replace_dict_list.append(dict)

if not already_exists:
    print "running"
    words = words.replace(replace_symbol, replace_letter)