2014-06-08 38 views
6

我是菜鳥,所以請原諒。根據用戶輸入替換列表中的項目

有三個列表

  1. 的字母A列表L = [ 'A', 'B', 'C', 'd', 'E']
  2. 數量N A列表= [ '1', '2', '3', '4', '5']
  3. 數量的字符串列表= [ '124', '351']

這些步驟的列表我希望實現

  1. 請求用戶的來信,例如A
  2. 查找字母列表中的字母L並記錄其數字位置,例如, [0]
  3. 使用數字列表中的相同數字位置N並記錄在那裏的數字 。 1
  4. 替換在非字母串中找到的數字的實例列表例如, ['124','351']變成['A24','35A']
  5. 詢問用戶下一個字母,直到所有數字字符串變成字母。

到目前爲止我所取得的成果是前4個步驟。第4步後,我想檢查數字字符串是否仍然包含數字,如果是這樣,進入第5步。我似乎無法弄清楚如何獲取代碼來檢查數字字符串是否包含更多數字。注意:號碼列表不限於數字。它可能包含數學符號,例如+或 -

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: ") 

我希望這樣可以。如果你需要什麼進一步的請讓我知道

+0

只要使用'while'循環並在檢查列表是否包含數字後獲取用戶輸入。如果您使用的是Python 2,請不要使用'input()',否則請使用'raw_input()'。 –

+2

@AbdulFatir:你怎麼知道OP沒有使用Python 3? Plus:英文單詞*是*'letters'。 「字母」是一組字母,如「拉丁字母」或「希臘字母」或「腓尼基字母」。 – DSM

+0

soo什麼時候該進程停止 –

回答

1
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: ") 

你可以改變的邏輯,縮短了代碼。

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"); 
+0

非常感謝您的幫助。我使用了兩個答案中的第一個,因爲它與我自己的代碼最接近,我想了解我的選項,但我仍然會嘗試使用其他代碼。我在這裏學習! :-) – Carl

+0

不用擔心,不客氣。第二個例子比第一個更整潔。 –

1

我似乎無法工作,如何讓代碼檢查數量 字符串包含更多數量的

你可以定義一個函數該循環通過列表來查看是否有任何條目都使用使用isdigit()像這樣

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

數字這將返回True如果任何在你的電話號碼字符串列表中的條目包含了一些


看到你的編輯

我的目標是爲它包含字母只

要做到這一點,你可以只檢查這樣

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

它使用isalpha()

真正

str.isalpha()

返回,如果字符串中的所有字符都 字母和至少有一個字符,否則爲false。

示範:

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

謝謝。我很欣賞這個建議,但是這個列表還可能包含其他數學字符,例如+或 - 。我認爲isdigit不會爲此工作? – Carl

+0

是的,掛上即時編輯 –

+0

很酷,我現在要試試這個。謝謝。 – Carl

相關問題