无法更换

时间:2014-11-19 12:34:40

标签: python replace

我需要用字母替换字符串中的1个符号,就像解码器游戏一样,但是使用我当前创建的代码。我需要开发程序的一部分,当有人输入符号和字母时,它用字母替换符号并保存列表,然后你继续这样做,直到所有的符号都被替换为创建一个单词。

编码字词列表:

 #+/084&"   
 #3*#%#+  
 8%203:  
 ,1$& 
 !-*%  
 .#7&33&  
 #*#71%  
 &-&641'2  
 #))85  
 9&330*  

未编码的单词列表:

 ACQUIRED  
 ALMANAC  
 INSULT   
 JOKE  
 HYMN  
 GAZELLE  
 AMAZON  
 EYEBROWS  
 AFFIX   
 VELLUM  

线索:

A#
M*
N%

代码:

#Import Section
import random
import string

class Game():

    def __init__(self):
        words_open = open('Words.txt')
        self.words = (words_open.read())               
        self.fixwords = self.words
        solved_open = open('solved.txt')
        self.solved = (solved_open.read())

    def menu_display(self):
        #Display the menu
        self.menud = ('''       MENU       
  1. Start Game
  2. Quit Game

  Please select 1 or 2''')
        self.menu()

    def menu(self):
        print self.menud
        #ask player to choose an option
        self.menu_choice = raw_input("  >>> ")
        print        
        self.menu_options()

    def menu_options(self):    
        #When menu option is selected
        if self.menu_choice == '1':
            self.s_game()
        elif self.menu_choice == '2':
            pass
        else:
            print "not valid input"
            self.menu()

    def s_game(self): 
        print 'Words:'
        print self.words
        self.clues()

    def clues(self):
        clue = raw_input('Do you want clues? please enter Yes or No '
                         '>>> ')
        clue = clue.upper()
        print clue
        clues_open = open('clues.txt')
        self.cluesclues = (clues_open.read())        
        if clue == 'YES':
            print '''Words:
            '''
            print self.words
            print 'Clues:'
            print self.cluesclues
            self.sorting()
        elif clue == 'NO':
            print self.words
            self.sorting()
        else:
            print "input is not valid"
            self.clues()


    def sorting(self):
        #if self.fixwords == 0:
            #self.fixwords = (words_open.read())
        #else:
            #pass
        self.symbol = raw_input("what symbol would you like to replace? >>> ")
        if self.symbol in self.fixwords:
            self.nletter = raw_input("that symbol is valid, what letter would you like to swap it with >>> ") 
            self.nletter.upper()
            #self.fixwords.replace(self.symbol, self.letter[, max])
            #self.fixwords.replace(self.symbol, self.nletter)
            string.replace(self.fixwords, self.symbol, self.nletter.upper)
            print self.fixwords
            print self.cluesclues
            self.prechange()
        elif self.symbol not in self.fixwords:
            print "That symbol is not valid or has already been changed"
            self.sorting()         

    def prechange(self):        
        self.change = raw_input("Do you want to change a letter for another letter? yes or no >>> ")
        self.change = self.change
        if self.change == "yes":
            self.changing()
        elif self.change == "no":
            self.sorting()
        else:
            print "that is not a valid input"
            self.prechange()

    def changing(self):
        self.chanlet = raw_input("what letter would you like to replace? >>> ")
        if self.chanlet in self.fixwords:
            self.letchan = raw_input("that letter is valid, what letter would you like to swap it with >>> ") 
            self.fixwords = string.replace(self.fixwords, self.chanlet, self.letchan)        
            print self.fixwords
            self.sorting()
        elif self.chanlet not in self.fixwords:
            print "That letter does not exist"
            self.changing()  

game = Game()
game.menu_display() 

1 个答案:

答案 0 :(得分:0)

你可以使用replace()函数。它可以替换字符串中的任何字符,即使它存在多次。您所要做的就是每当用户选择要替换的符号和一个字母时,您在编码的单词列表上循环并用该字母替换该符号:

symbol = input("Enter the symbol to replace : ")
letter = input("Enter the letter : ")
for i in range(0, len(encoded_list)) :
     encoded_list[i] = encoded_list[i].replace(symbol, letter, len(encoded))
相关问题