![](/img/trans.png)
[英]How do you get this to remove the value that the user wants to remove from the list and then return the new list values
[英]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)
您正在調用變量start
但chosen
的變量是帶有字母列表的變量。 所以這應該工作:
keptLetters = chosen.remove (removeLetter)
您的代碼中有幾個問題。 我冒昧地為您提供了更多pythonic。
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"
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")
[]
而不是括號()
表示列表來更改列表。 map()
函數將所有結果都放入一個列表中。 chosen
應該保留一個列表,而不是字符串,因為將來您將通過刪除字母來對其進行操作。 join()
所有在列表中的項目一起由單個空格分隔字符串" "
。 list.remove()
可以在值不在列表中時發回ValueError
,因此您需要在try..except
塊中進行處理。 while
循環包圍您的邏輯,以不斷要求刪除一個字母。 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.