简体   繁体   中英

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

I am working on this code and I cannot get the "removeLetter" to delete the letter the user chooses from the "chosen" list. I figured out I can't use list functions with strings, but I don't know how to go about making the start.remove() code to work without converting the user's string input to match the item he/she would like to be removed from the "chosen" list. Can someone help me please?

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)

You have an error in your code specifically in this line:

keptLetters = start.remove (removeLetter)

You are calling the variable start but the variable chosen is the one with the list of letters. So this should work:

keptLetters = chosen.remove (removeLetter)

You have a few issues in your code. I took the liberty to make it more pythonic for you.

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. Having multiple lists, and repeating your code is not very pythonic. So I've made the "lists" as a tuple of tuples (since you may not plan on modifying this). If so, then change to lists by denoting them with square brackets [] instead of parentheses () .
  2. Since you're performing the same operation on multiple lists, you can use the map() function to get the results from all of them into a list.
  3. chosen should remain a list, instead of a string, since you'll be manipulating it in the future by removing a letter.
  4. You can easily join() all the items in a list together as a string separated by a single space " " .
  5. Since list.remove() can send back a ValueError if the value isn't in the list, you'll want to handle that in a try..except block.
  6. Can surround your logic with a while loop to keep asking for a letter to remove if the previous attempts failed.
  7. chosen.remove changes the original array, so you don't need to save it into keptLetters .

I hope this helps you out!

Edit: Added equivalent Python 2 code

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM