Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replace an item in a list based on user input

Tags:

python

string

I'm a noob so please excuse me.

There are three lists

  1. A list of letters L = ['A','B','C','D','E']
  2. A list of numbers N = ['1','2','3','4','5']
  3. A list of number strings List = ['124','351']

These are the steps I wish to achieve

  1. Request a letter from the user e.g. A
  2. Find the letter in the letter list L and record its numerical position e.g. [0]
  3. Use the same numeric position in the list of numbers N and record the number that's there e.g. 1
  4. Replace the instances of the number found in the non-letter strings List e.g. ['124','351'] becomes ['A24','35A']
  5. Ask the user for the next letter until all the number strings become letters.

What I have achieved so far is the first 4 steps. After step 4 I thought to check if the number strings still contained numbers and if so go to step 5. I can't seem to work out how to get the code to check if the number strings contain any more number. NOTE: The number list is not limited to numbers. It could contain math symbols e.g. + 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 that is OK. If you need anything further please let me know

like image 385
Carl Avatar asked Jun 08 '14 13:06

Carl


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 could 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");
like image 135
Padraic Cunningham Avatar answered Sep 28 '22 20:09

Padraic Cunningham


I can't seem to work out how to get the code to check if the number strings contain any more number

You could define a function that loops over the list to see if any of the entries have digits using using isdigit() like so

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 your number string list contains a number


Saw your edit

My goal is for it to contain letters only

To do that you could just check like this

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

This uses isalpha()

str.isalpha()

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

Demonstration:

>>> all(x.isalpha() for x in ['abc', 'def'])
True
>>> all(x.isalpha() for x in ['ab1', 'def'])
False
like image 25
Tim Avatar answered Sep 28 '22 22:09

Tim