Is there a way to check the first letter of every word and delete a word based on user input in python?

Question:

So I have this program thats supposed to guess which NBA player you are thinking of (In this example GSW). I want to have the user input the first letter of the player and then remove the rest from the list, besides player that also start with the same letter. I was looking around but there was nothing that I could find that works for this code.

Ex. Player Name: (Stephen Curry)
Input: 
          Guard
          Yes
          S
Output: 
Your Player is Stephen Curry
(Didn't really get to that part)
#Introduce the game
print("Welcome to the NBA guessing game, All you have to do is think of an NBA player in the Warriors in the current season and answer some questions below with a yes no answer. If I guess your player I win if I don't you win. You ready?")
#What position does the player play, get input
print("Did you think of an NBA Player on the Golden State Warriors. What position does he play: guard, forward, or center")
pos = input().lower().strip(" ,.?!@#$%^&*()_+{}|:<>?")
#Roster order lists , PG, SG, SF, PF, C, will be the groups in the game
gswG = ["stephencurry", "jordanpoole", "dontedivincenzo", "macmcclung", "mosesmoody", "klaythompson", "jeromerobinson", "ryanrollins", "patspencer", "quinndaryweatherspoon"]
gswF = ["patrickbaldwinjr", "draymondgreen", "jamychalgreen", "andreigoudala", "jonathankuminga", "guisantos", "andrewwiggins"]
gswC = ["kevonlooney", "trevionwilliams", "jameswiseman"]
#Make an if statement for each position
if pos == "guard":
    print("So your player is a Guard")
    #Print the list and ask if its either the first few or last few on the list
    print("Is your player on the first 5 spots of the list")
    first = input().lower().strip("~`!@$#%^&*()_+-={}|:<>?,./';[]=-abcdfghijklmpqrtuvwxz")
    if first == "yes":
        gswG = gswG.remove["klaythompson", "jeromerobinson", "ryanrollins", "patspencer", "quinndaryweatherspoon"]
        print("Ok so it is between: " + gswG)
        #Ask what letter the players name starts with
        print("What is the first chatacter of that players name")
        fchar = input().lower().strip("~`!@$#%^&*()_+-={}|:<>?,./';[]=")

The code is partly finished I just want to get this working because the next parts will be the same, just for the opposite answer.

Asked By: TheCPPGuy

||

Answers:

I hope I got correctly what you’re trying to do. One way could be using list comprehension.
For example, if I have the starting list and the input from the player:

name = ['Mike','Marie','Karl','Stephen','Melanie']
letter = input('Please insert a lettern>>')

and I go for

names_1 = [x for x in name if x[0] == letter.upper()]

names_1 will be a list containing all the names in name starting with the selected letter.
Ofc you should retain the "letter.upper()" if the names have capital first letter in the list, otherwise you could co to letter.lower() ( just in case the player insert a capital letter in the input ).
I hope it’s useful for you!

Answered By: Davide P

Is there a way to check the first letter of every word

u can use string slicing. for example:

string = "abcdefghijkl"

print(string[2]) # c
print(string[0]) # a

string[number] the number is the index you wanted to check

and delete a word based on user input in python?

if you want to delete the input word in the string:

...
# prompt user input
user_input = input("some words")

# check if the input words in the string
if user_input in string:

# replace the words with empty string (so it is removed)
    string.replace(user_input, "")
Answered By: uwuK

I made a couple of changes in your script to make it more readable and to fix a couple of errors… see inline notes

print("Welcome to the NBA guessing game, All you have to do is think of an NBA player in the Warriors in the current season and answer some questions below with a yes no answer. If I guess your player I win if I don't you win. You ready?")

gswG = ["stephencurry", "jordanpoole", "dontedivincenzo", 
        "macmcclung", "mosesmoody", "klaythompson", 
        "jeromerobinson", "ryanrollins", "patspencer",
        "quinndaryweatherspoon"]
gswF = ["patrickbaldwinjr", "draymondgreen", "jamychalgreen",
        "andreigoudala", "jonathankuminga", "guisantos",
        "andrewwiggins"]
gswC = ["kevonlooney", "trevionwilliams", "jameswiseman"]

print("Did you think of an NBA Player on the Golden State Warriors. What position does he play: guard, forward, or center")

pos = input().lower().strip()


if pos == "guard":
    print("So your player is a Guard")
    print("Is your player on the first 5 spots of the list")

    first = input().lower().strip()
    if first == "yes":
        # you can simply slice the list instead of calling 
        # remove a bunch of times.
        gswG = gswG[:6]   

        # need to convert the list into a string before 
        # you can add it to another string
        print("Ok so it is between: " + str(gswG))  

        #Ask what letter the players name starts with
        print("What is the first chatacter of that players name")
        
        fchar = input().lower().strip()
        # use list comprehension to select the players with the 
        # correct first letter.
        gswG = [player for player in gswG if player[0] == fchar]
        [print(i,end=" ") for i in gswG]
Answered By: Alexander

A simple way to do that:

names = ["Chuck","Stephen", "Carl", "John"]
letter = input() #example: "C"

lst = list(filter(lambda name: False if name.startswith(letter) else True ,names))
print(lst) #output: ["Stephen","John"]
Answered By: Cezar Peixeiro
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.