I'm a noob so please excuse me.
There are three lists
These are the steps I wish to achieve
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
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");
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With