Python 如何删除用户';什么是列表中的输入?

Python 如何删除用户';什么是列表中的输入?,python,Python,我正在编写此代码,无法使用“removeLetter”删除用户从“Selected”列表中选择的字母。我发现我不能对字符串使用列表函数,但我不知道如何制作 start.remove()代码,无需将用户的字符串输入转换为与他/她希望从“所选”列表中删除的项匹配即可工作。有人能帮我吗 import random oneList = "a ", "b ", "c " twoList = "d ", "e ", "f " threeList = "g ", "h ", "i " fourList =

我正在编写此代码,无法使用“removeLetter”删除用户从“Selected”列表中选择的字母。我发现我不能对字符串使用列表函数,但我不知道如何制作 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
,但选择的变量
是包含字母列表的变量。因此,这应该是可行的:

keptLetters = chosen.remove (removeLetter)

您的代码中有一些问题。我冒昧地让它更适合你

Python 2 Python 3
  • 有多个列表,并且重复您的代码不是很符合Python。因此,我将“列表”作为元组的元组(因为您可能不打算修改它)。如果是这样,则用方括号
    []
    代替括号
    ()
    来表示列表
  • 由于您在多个列表上执行相同的操作,因此可以使用该函数将所有列表的结果放入一个列表中
  • selected
    应该是一个列表,而不是一个字符串,因为将来您将通过删除一个字母来操作它
  • 您可以轻松地将列表中的所有项目合并为一个字符串,并用一个空格分隔
  • 由于如果值不在列表中,您可以发回
    ValueError
    ,因此您需要在块中处理该值
  • 如果以前的尝试失败,可以用循环来围绕逻辑,不断请求删除一封信
  • 已选择。删除
    更改原始数组,因此无需将其保存到
    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)
    

    您可能会被绊倒,因为
    oneList
    fourList
    不是列表,而是元组。重要的是,列表可以变异(例如,可以从列表中删除项),元组不能。列表被
    []
    包围,尝试运行
    type()
    ,您将看到@Kevin在说什么,而不是执行
    start.remove
    ,你可能想从
    选择的
    中进行切分。问题还不止这些。特别是因为所选的
    是一个字符串。@Anderson虽然这是您指出的一个有效错误,但我仍然有问题。我应该意识到我也像其他人说的那样使用元组。你还有其他建议吗?看这个,我很感激,我想我理解你的意思。但是,我在你的代码中发现了这个错误。回溯(最后一次调用):removeLetter=input中的文件“G:\lettersTwo.py”,第25行(“您要删除哪个字母?”)文件“”,第1行,在名称中错误:未定义名称“b”是否确实正确复制了代码?我假设您正在运行Python 3。我为此做了一个道歉。好吧,对不起。我实际上是在运行Python2。我不知道为什么我还有它。我确实安装了3个。你的计划成功了,谢谢@NickAndrulewicz,我更新了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 = 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")
    
    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)