python - Having trouble replacing with -
i need replace 1 symbol in string letter, decoder game, code have created. need develop part of program when inputs symbol , letter replaces symbol letter , saves list, keep doing untill symbols have been replaced create word.
the list of encoded words:
#+/084&" #3*#%#+ 8%203: ,1$& !-*% .#7&33& #*#71% &-&641'2 #))85 9&330*
the list of words uncoded:
acquired almanac insult joke hymn gazelle amazon eyebrows affix vellum
clues:
a# m* n%
code:
#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 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 choose option self.menu_choice = raw_input(" >>> ") print self.menu_options() def menu_options(self): #when menu option 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 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 not valid" self.clues() def sorting(self): #if self.fixwords == 0: #self.fixwords = (words_open.read()) #else: #pass self.symbol = raw_input("what symbol replace? >>> ") if self.symbol in self.fixwords: self.nletter = raw_input("that symbol valid, letter swap >>> ") 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 not valid or has been changed" self.sorting() def prechange(self): self.change = raw_input("do want change letter letter? yes or no >>> ") self.change = self.change if self.change == "yes": self.changing() elif self.change == "no": self.sorting() else: print "that not valid input" self.prechange() def changing(self): self.chanlet = raw_input("what letter replace? >>> ") if self.chanlet in self.fixwords: self.letchan = raw_input("that letter valid, letter swap >>> ") 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 not exist" self.changing() game = game() game.menu_display()
you can use replace() function. can replace character in string if exists several times. have each time user select symbol replace , letter, loop on list of encoded words , replace symbol letter :
symbol = input("enter symbol replace : ") letter = input("enter letter : ") in range(0, len(encoded_list)) : encoded_list[i] = encoded_list[i].replace(symbol, letter, len(encoded))
Comments
Post a Comment