Eric Cuevas
Eric Cuevas

Reputation: 63

Avoiding to print out a certain word in a list

So I'm just starting to learn python. I am doing this exercise which asks the following:

"Implement a program that requests a list of words from the user and then prints each word in the list that is not 'secret'."

This is what I have so far but it seems that it is not working when the code is executed. Does anyone have any ideas?

wordinput = input('Enter a list of words: ')

def keep_secret(l: wordinput) -> list:
    for i in wordinput:
        if i == 'secret':
            return None
        else:
            print (i)

Upvotes: 2

Views: 1947

Answers (3)

Chinh Nguyen
Chinh Nguyen

Reputation: 21

I also ran into this problem as well. Thanks to John La Rooy's solution, here is my take in a shorter version:

wordinput = input('Enter a list of words: ').split(', ')

for i in wordinput:
        if i != 'secret':
            print (i)

Upvotes: 1

John La Rooy
John La Rooy

Reputation: 304355

I think you are trying to do this

wordinput = input('Enter a list of words: ').split()

def keep_secret(l: wordinput) -> list:
    for i in wordinput:
        if i == 'secret':
            continue 
        else:
            print (i)

You can simplify to this

def keep_secret(l: wordinput) -> list:
    for i in wordinput:
        if i != 'secret':
            print (i)

Or return the filtered list like this

def keep_secret(l: wordinput) -> list:
    return [i for i in wordinput if i != 'secret']

print(keep_secret(wordinput))

Upvotes: 1

JoeC
JoeC

Reputation: 1850

Remove your return statement, you don't wanna return anything otherwise it will terminate the program by returning None

Upvotes: 0

Related Questions