我正在处理此代码,但无法获取“ 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)
答案 0 :(得分:0)
您的代码中有错误,特别是在此行中:
new
您正在调用变量keptLetters = start.remove (removeLetter)
,但是变量start
是带有字母列表的变量。所以这应该工作:
chosen
答案 1 :(得分:0)
您的代码中有一些问题。我冒昧地为您提供了更多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代码
答案 2 :(得分:-1)
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)