my question is related to python. I have a .txt file that contains some text. I want to have a code that prompts the user to enter a string of forbidden letters and then print the number of words in the file that don’t contain any of them. I wrote this one:
letters=raw_input("Please enter forbidden letters")
text=open("p.txt", "r").read().split()
for word in text:
for i in letters:
if not j in word:
print word
and also have this one:
letters=raw_input("Please enter forbidden letters")
text=open("p.txt", "r").read().split()
for word in text:
for i in letters:
for j in i:
if not j in word:
print word
But both codes give me words that don't contain each of characters and not all characters. for example if my file have "Hello good friends" and user entered "oh", this codes print:
Hello good friends friends
But I expect just "friends" that don't have nor "o" not "h". and expect to have "friends" one time and not two times. How can I fix my problem?
Put them in a set to remove dupes. Then for each word you want to print it only if all letters do not appear in the word. Something like this:
letters = raw_input("Please enter forbidden letters")
words = set(open("p.txt", "r").read().split())
for word in words:
if all(letter not in word for letter in letters):
print word