I created a function that takes a word and a string of 'forbidden' letters, and returns True if the word doesn't use any of the letter.
def avoids(word,forbidden):
for letter in word:
if letter in forbidden:
retu开发者_如何学Crn False
else:
return True
I want to modify this so instead of using 'forbidden' as the letters to avoid, the user will be prompted to place a couple of letters and print the number of words that don't contain any of them. I also have a .txt document that contain these words to make it interesting.
This is what I've came up with which is wrong. I would like some assistance and education if possible since my 'online' teacher is never around to assist. Please Help :)
def word_no_forbidden():
forbidden = input('pick 5 letters')
fin = open('words1.txt')
no_forbidden_word = 0
for line in fin:
word = line.strip()
for letter in word:
if forbidden in word:
continue
print word
This is the error I get and I understand it but, I'm not sure how else to approach this...
Traceback (most recent call last):
File "<pyshell#5>", line 1, in <module>
word_no_forbidden()
File "C:/Python27/test.py", line 9, in word_no_forbidden
if forbidden in word:
TypeError: 'in <string>' requires string as left operand, not tuple
def word_no_forbidden():
forbidden = raw_input('pick 5 letters')
fin = open('words1.txt')
no_forbidden_word = 0
for line in fin:
word = line.strip()
for letter in list(word):
if letter in forbidden:
break
else:
print word
NOTE:
1> As winston said use raw_input
2> In case you want to traverse a string use list(your_string)
to get a list of characters
3> the else
here executes only when our for letter in list(word)
loop completes without ever going to break( in other words, none of the letters are in forbidden)
My guess...
You are python 2.x
When you ran the program you typed in:
'a','b','c','d'
On python 2.x, you want to use raw_input not input. That'll give a string of exactly what you type in. As it is python will try to interpret whatever you right as a python expression which is dangerous and generally a bad idea.
Your second problem is that you've reversed your line of code from the first example, letter in forbidden
, so that it becomes forbidden in word
Not the same thing.
To read in a string from the user, you should use raw_input
, not input
. input
tries to actually evaluate the string the user types as Python code, and can result in you getting a datatype you don't expect (or other, worse things).
In contrast, raw_input
always returns a string.
(Note: this applies for Python 2.x only. Starting with Python 3, raw_input
is renamed to input
, and there's no function that does what input
used to do.)
In this part of the code
for letter in word:
if forbidden in word:
You should be doing
for letter in forbidden:
if letter in word:
What you want to do is check, for every letter in the ones the users entered, if it is in the word read from the file, skip the word and go to the next one. There are more pythonic ways of doing this but I think this is clear and easy to understand.
精彩评论