簡體   English   中英

如何從列表中刪除用戶的輸入?

[英]How do you remove a user's input from a list?

我正在處理此代碼,但無法獲取“ removeLetter”來刪除用戶從“選擇”列表中選擇的字母。 我發現我不能將列表函數與字符串一起使用,但是我不知道如何在不轉換用戶的字符串輸入以匹配他/她想要的項目的情況下使start.remove()代碼正常工作從“選擇”列表中刪除。 有人能幫助我嗎?

import random

oneList = "a ", "b ", "c "
twoList = "d ", "e ", "f "
threeList = "g ", "h ", "i "
fourList = "j ", "k ", "l "

# Selects a letter at random from each list
oneRandom = random.choice(oneList)
twoRandom = random.choice(twoList)
threeRandom = random.choice(threeList)
fourRandom = random.choice(fourList)

# Displays chosen letter from each list
print("These are your letters.")
chosen = oneRandom + twoRandom + threeRandom + fourRandom
print(chosen)

# First user input
start  = input("Would you like to remove a letter? y or n? ")

# If start = yes then do this.
if start == 'y':
    removeLetter = input("What letter would you like to remove? ")

    # Removes user's chosen letter.
    keptLetters = chosen.remove(removeLetter)

    # Displays kept letters.
    print(keptLetters)

您的代碼中有錯誤,特別是在此行中:

keptLetters = start.remove (removeLetter)

您正在調用變量startchosen的變量是帶有字母列表的變量。 所以這應該工作:

keptLetters = chosen.remove (removeLetter)

您的代碼中有幾個問題。 我冒昧地為您提供了更多pythonic。

Python 2

import random

letter_sets = (("a", "b", "c"),
               ("d", "e", "f"),
               ("g", "h", "i"),
               ("j", "k", "l"))

# Selects a letter at random from each list
chosen = map(random.choice, letter_sets)

# Displays chosen letter from each list
print "These are your letters."
print " ".join(chosen)

# First user input
start = raw_input("Would you like to remove a letter? y or n?")

# If start = yes then do this.
if start[0].lower() == 'y':
    while(len(chosen) == 4): #Keep looping until a letter is chosen
        removeLetter = raw_input("What letter would you like to remove?")
        try:
            # Removes user's chosen letter.
            chosen.remove(removeLetter)
            # Displays kept letters.
            print " ".join(chosen)
        except ValueError: #If removeLetter is not in chosen
            print removeLetter, "is not in the list of letters"

Python 3

import random

letter_sets = (("a", "b", "c"),
               ("d", "e", "f"),
               ("g", "h", "i"),
               ("j", "k", "l"))

# Selects a letter at random from each list
chosen = list(map(random.choice, letter_sets))

# Displays chosen letter from each list
print("These are your letters.")
print(" ".join(chosen))

# First user input
start = input("Would you like to remove a letter? y or n?")

# If start = yes then do this.
if start[0].lower() == 'y':
    while(len(chosen) == 4): #Keep looping until a letter is chosen
        removeLetter = input("What letter would you like to remove?")
        try:
            # Removes user's chosen letter.
            chosen.remove(removeLetter)
            # Displays kept letters.
            print(" ".join(chosen))
        except ValueError: #If removeLetter is not in chosen
            print(removeLetter, "is not in the list of letters")
  1. 擁有多個列表,並重復執行代碼不是很Python。 因此,我將“列表”作為元組的元組(因為您可能不打算對其進行修改)。 如果是這樣,則通過用方括號[]而不是括號()表示列表來更改列表。
  2. 由於您要在多個列表上執行相同的操作,因此可以使用map()函數將所有結果都放入一個列表中。
  3. chosen應該保留一個列表,而不是字符串,因為將來您將通過刪除字母來對其進行操作。
  4. 您可以輕松地join()所有在列表中的項目一起由單個空格分隔字符串" "
  5. 由於list.remove()可以在值不在列表中時發回ValueError ,因此您需要在try..except塊中進行處理。
  6. 如果先前的嘗試失敗,可以用while循環包圍您的邏輯,以不斷要求刪除一個字母。
  7. chosen.remove更改了原始數組,因此您無需將其保存到keptLetters

我希望這可以幫助你!

編輯:添加了等效的Python 2代碼

Je Pense que sa doitêtreça

import random oneList = "a ", "b ", "c "
twoList = "d ", "e ", "f " 
threeList = "g ", "h ", "i " 
fourList = "j ", "k ", "l " 
# Selects a letter at random from each list 
oneRandom = random.choice(oneList) 
twoRandom = random.choice(twoList) 
threeRandom = random.choice(threeList) 
fourRandom = random.choice(fourList) 
# Displays chosen letter from each list 
print("These are your letters.") chosen = oneRandom + twoRandom + threeRandom + fourRandom print(chosen) 
# First user input 
start = input("Would you like to remove a letter? y or n? ") 
# If start = yes then do this. 
if start == 'y': removeLetter = input("What letter would you like to remove? ") 
# Removes user's chosen letter. 
keptLetters = chosen.remove(removeLetter)
# Displays kept letters. 
print(keptLetters)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM