python - Replace an item in a list based on user input -
i'm noob please excuse me.
there 3 lists
- a list of letters l = ['a','b','c','d','e']
- a list of numbers n = ['1','2','3','4','5']
- a list of number strings list = ['124','351']
these steps wish achieve
- request letter user e.g. a
- find letter in letter list l , record numerical position e.g. [0]
- use same numeric position in list of numbers n , record number that's there e.g. 1
- replace instances of number found in non-letter strings list e.g. ['124','351'] becomes ['a24','35a']
- ask user next letter until number strings become letters.
what have achieved far first 4 steps. after step 4 thought check if number strings still contained numbers , if go step 5. can't seem work out how code check if number strings contain more number. note: number list not limited numbers. contain math symbols e.g. + or -
l = ['a','b','c','d','e'] n = ['1','2','3','4','5'] list = ['124','351'] print ("enter letter") # there number in list # if yes following else print list # ask letter user letter = input ("enter letter: ") # confirm whether letter correct or not if letter in l: # find position of letter in list position = (l.index(letter)); # make variable called number value @ same position in n list number = n[position]; # replace numbers in list letter entered list = [item.replace(number, letter) item in list]; # print list numbers replaced print (list, "\n"); print ("please guess again. \n"); letter = input ("enter letter now: ") # repeat until list contains letters else: print ("that not correct"); print ("please guess again. \n"); letter = input ("enter letter now: ")
i hope ok. if need further please let me know
l = ['a','b','c','d','e'] n = ['1','2','3','4','5'] n_strings = ['124','351'] # don't use list variable name while not all( x.isalpha() x in n_strings): # keep going until alpha chars print ("enter letter") # there number in list # if yes following else print list # ask letter user letter = input("enter letter: ") # confirm whether letter correct or not if letter in l: # find position of letter in list position = (l.index(letter)); # make variable called number value @ same position in n list number = n[position]; # replace numbers in list letter entered n_strings = [item.replace(number, letter) item in n_strings]; # print list numbers replaced print (n_strings, "\n"); print ("please guess again. \n"); letter = input("enter letter now: ") # repeat until list contains letters else: print ("that not correct"); print ("please guess again. \n"); letter = input("enter letter now: ")
you change logic , shorten code.
while true: if all(x.isalpha() x in n_strings ): print("all guessed correct {}".format(n_strings)) # if alpha print final n_string , break out of loop break print n_strings letter = input("please enter letter: ") if letter in l: # find position of letter in list position = (l.index(letter)); number = n[position]; n_strings = [item.replace(number, letter) item in n_strings]; print (n_strings, "\n"); # repeat until list contains letters else: print ("that not correct"); print ("please guess again. \n");
Comments
Post a Comment