hello,

Pls can anybody help me with a folloving exercise? (I must say that I am really beginer in this field, learning by myself and with daniweb (if sb gives me help))

Pompt the user to enter a string of forbidden letters and also to enter a words. Make a script which shows just the words which don't contains the forbidden letters.

My not fulfiled version is below (It's not finished beacause I don't know how to continue. I am able to go one by one (letter) and verify if the words contains the forbidden letters. Another problem occure if I enter any words, for example : Hello, my name is Vlady => comma and space is considered as fault.
I haven't started to study a topic: List (which is following) so I am not sure if this exercise is possible to solve using different method, thought I did something similar but a text was in list like ).

def avoids():
    input1 = raw_input('enter forbidden letters')
    input2 = raw_input('enter words')
    for letter in input2:
        if letter in input1:
            print 'false'
        else:
            print 'true'

avoids()

Thank you

Vlady

hello,

Pls can anybody help me with a folloving exercise? (I must say that I am really beginer in this field, learning by myself and with daniweb (if sb gives me help))

Pompt the user to enter a string of forbidden letters and also to enter a words. Make a script which shows just the words which don't contains the forbidden letters.

My not fulfiled version is below (It's not finished beacause I don't know how to continue. I am able to go one by one (letter) and verify if the words contains the forbidden letters. Another problem occure if I enter any words, for example : Hello, my name is Vlady => comma and space is considered as fault.
I haven't started to study a topic: List (which is following) so I am not sure if this exercise is possible to solve using different method, thought I did something similar but a text was in list like ).

def avoids():
    input1 = raw_input('enter forbidden letters')
    input2 = raw_input('enter words')
    for letter in input2:
        if letter in input1:
            print 'false'
        else:
            print 'true'

avoids()

Thank you

Vlady

I've been working on this exercise and I come up with this possibility but is not for 100% what is demanded in the task. Here is it:

def avoids():
##    input1 = raw_input('enter forbidden letters')
    input2 = raw_input('enter words')
    input2 = [input2]
    for text in input2:
        word_list = text.split()
        for word in word_list:
            if 'a' in word: # here I'd like to put input1
                continue
            print word

avoids()

Thing is that it works just for one forbidden letter and not for more (input1) which is the case. I don't know how to make it work for more forbidden letters (which are prompted). In this script is that command disabled.
Pls give me the hint for this problem.

many thanks

vlady

Almost there, you could do it like this ...

def avoid(forbidden_letters, word):
    for letter in forbidden_letters:
        print letter  # for test only
        if letter in word:
            return True

# test data
forbidden_letters = "mvy"
words = "house mouse validate kitchen honey love"

# pull out the individual word from words
for word in words.split():
    if avoid(forbidden_letters, word):
        print word, "contains a forbitten letter"

It's better to use fixed test data and later go to user input.

Edited 6 Years Ago by vegaseat: user

Almost there, you could do it like this ...

def avoid(forbidden_letters, word):
    for letter in forbidden_letters:
        print letter  # for test only
        if letter in word:
            return True

# test data
forbidden_letters = "mvy"
words = "house mouse validate kitchen honey love"

# pull out the individual word from words
for word in words.split():
    if avoid(forbidden_letters, word):
        print word, "contains a forbitten letter"

It's better to use fixed test data and later go to user input.

I thought that I understand but no, I haven't still finished this task. I am stucked.

So, what are you stuck with?
It may be best to start a new thread with a meaningful title.

Edited 6 Years Ago by vegaseat: n/a

So, what are you stuck with?
It may be best to start a new thread with a meaningful title.

ok, I am working on it. I've just wanted to say that I don't understand your last script. At first when I saw it, I had a idea, but it didn't work.
here is my latest version, there are not much changes. I use (commands) which I know, thought there are not much.
In this script I get quite reverse, the words which I don't want to have shown. I 'd like to have just: ' this is test'

def avoid():
##    input1 = raw_input('enter words')
##    input2 = raw_input('enter forbidden words')

    words = 'hello this is my test'
    forbidden = 'l, y'

    for word in words.split():
        for letter in forbidden:
##            print letter,
            if letter in word:
##                continue
                print word


avoid()

ok, I am working on it. I've just wanted to say that I don't understand your last script. At first when I saw it, I had a idea, but it didn't work.
here is my latest version, there are not much changes. I use (commands) which I know, thought there are not much.
In this script I get quite reverse, the words which I don't want to have shown. I 'd like to have just: ' this is test'

def avoid():
##    input1 = raw_input('enter words')
##    input2 = raw_input('enter forbidden words')

    words = 'hello this is my test'
    forbidden = 'l, y'

    for word in words.split():
        for letter in forbidden:
##            print letter,
            if letter in word:
##                continue # here I want to skip to another word
            print word


avoid()

I use continue, thought that if the condition is fulfiled, it will skip to another word. finaly I will get the words without forbidden words.

I use continue, thought that if the condition is fulfiled, it will skip to another word. finaly I will get the words without forbidden words.

finaly I got the answer, here is it and it' s easy.

def avoid():
    words = raw_input('enter words')
    forbidden = raw_input('enter forbidden words')

##    words = 'hello this is my test'
##    forbidden = 'l, y'

    for word in words.split():
        for letter in forbidden:
            if letter in word:
                word=""
            else:
                word=word
        if word=="":
            pass
        else:
            print word
avoid()
This question has already been answered. Start a new discussion instead.