我是菜鳥,所以請原諒。根據用戶輸入替換列表中的項目
有三個列表
- 的字母A列表L = [ 'A', 'B', 'C', 'd', 'E']
- 數量N A列表= [ '1', '2', '3', '4', '5']
- 數量的字符串列表= [ '124', '351']
這些步驟的列表我希望實現
- 請求用戶的來信,例如A
- 查找字母列表中的字母L並記錄其數字位置,例如, [0]
- 使用數字列表中的相同數字位置N並記錄在那裏的數字 。 1
- 替換在非字母串中找到的數字的實例列表例如, ['124','351']變成['A24','35A']
- 詢問用戶下一個字母,直到所有數字字符串變成字母。
到目前爲止我所取得的成果是前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: ")
我希望這樣可以。如果你需要什麼進一步的請讓我知道
只要使用'while'循環並在檢查列表是否包含數字後獲取用戶輸入。如果您使用的是Python 2,請不要使用'input()',否則請使用'raw_input()'。 –
@AbdulFatir:你怎麼知道OP沒有使用Python 3? Plus:英文單詞*是*'letters'。 「字母」是一組字母,如「拉丁字母」或「希臘字母」或「腓尼基字母」。 – DSM
soo什麼時候該進程停止 –