Replace an item in a list based on user input

I am noob, so please excuse me.

There are three lists.

  • List of letters L = ['A', 'B', 'C', 'D', 'E']
  • List of numbers N = ['1', '2', '3', '4', '5']
  • List of string numbers List = ['124', '351']

These are the steps that I want to achieve.

  • Request a letter from the user, for example. A
  • Find the letter in the list of letters L and write down its numerical position, for example. [0]
  • Use the same digital position in the list of numbers N and write down the number, which, for example, 1
  • Replace instances of the number found in non-letter strings List , for example. ['124', '351'] becomes ['A24', '35A']
  • Ask the user about the next letter until all the number lines are letters.

What I have achieved so far is the first 4 steps. After step 4, I decided to check if the lines of numbers contain numbers, and if so, go to step 5. I can’t figure out how to get the code to check if the number of lines contains more than a number. NOTE. The list of numbers is not limited to numbers. It may contain mathematical symbols, for example. + or -

L = ['A','B','C','D','E'] N = ['1','2','3','4','5'] list = ['124','351'] print ("Enter a letter") # Is there a number in List # If yes then do the following else print List # Ask for a letter from the user letter = input ("Enter letter: ") # Confirm whether the letter is correct or not if letter in L: # Find the position of the letter in the list position = (L.index(letter)); # Make a variable called number with value at the same position in the N list number = N[position]; # Replace the numbers in the List with the letter entered list = [item.replace(number, letter) for item in list]; # Print the list with the numbers replaced print (list, "\n"); print ("Please guess again. \n"); letter = input ("Enter a letter now: ") # repeat until the List only contains letters else: print ("That is not correct"); print ("Please guess again. \n"); letter = input ("Enter a letter now: ") 

I hope everything is in order. If you need anything, please let me know.

+6
source share
2 answers
 L = ['A','B','C','D','E'] N = ['1','2','3','4','5'] n_strings = ['124','351'] # Don't use list as a variable name while not all( x.isalpha() for x in n_strings): # keep going until all are alpha chars print ("Enter a letter") # Is there a number in List # If yes then do the following else print List # Ask for a letter from the user letter = input("Enter letter: ") # Confirm whether the letter is correct or not if letter in L: # Find the position of the letter in the list position = (L.index(letter)); # Make a variable called number with value at the same position in the N list number = N[position]; # Replace the numbers in the List with the letter entered n_strings = [item.replace(number, letter) for item in n_strings]; # Print the list with the numbers replaced print (n_strings, "\n"); print ("Please guess again. \n"); letter = input("Enter a letter now: ") # repeat until the List only contains letters else: print ("That is not correct"); print ("Please guess again. \n"); letter = input("Enter a letter now: ") 

You can change the logic and shorten the code.

 while True: if all(x.isalpha() for x in n_strings ): print("All guessed correct {}".format(n_strings)) # if all are alpha print final n_string and break out of loop break print n_strings letter = input("Please enter a letter: ") if letter in L: # Find the position of the letter in the list position = (L.index(letter)); number = N[position]; n_strings = [item.replace(number, letter) for item in n_strings]; print (n_strings, "\n"); # repeat until the List only contains letters else: print ("That is not correct"); print ("Please guess again. \n"); 
+1
source

I can’t figure out how to get the code to check if the line number contains any number

You can define a function that loops through the list cyclically to see if any of the entries has digits using isdigit () , as shown

 def has_number(lst): for s in lst: if any(x.isdigit() for x in s): return True return False 

This will return True if any of the entries in the list of number strings contains a number


See your editing

My goal is to only contain letters

To do this, you can simply check it as

 if all(x.isalpha() for x in lst): # lst contains only entries that consists of letters 

Used by isalpha ()

str.isalpha ()

Returns true if all the characters in the string are alphabetic and there is at least one character, otherwise false.

Demonstration:

 >>> all(x.isalpha() for x in ['abc', 'def']) True >>> all(x.isalpha() for x in ['ab1', 'def']) False 
+1
source

Source: https://habr.com/ru/post/970538/


All Articles