Python Forum

Full Version: def search_for_vowels():
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
def search4vowels(word):
    vowels = set('aeiou')
    found = vowels.intersection(set(word))
    for vowel in found:
        print(vowel)
Gives a Traceback name error when trying to invoke: "name 'search4vowels' not defined. This came from 'Head First Python'. And I know, using numbers in the function name isn't the best practice. It's the way it is in the book.
The code you've provided defines a function. It doesn't do anything else. Barring a SyntaxError, you're not really going to get errors from just defining a function. Please provide your full code, that actually produces (I expect) a NameError. If your full code is long (>10 lines) I highly suggest you simplify your code until it's as simple as possible while reproducing your issue.
(Jan-07-2020, 12:38 AM)micseydel Wrote: [ -> ]The code you've provided defines a function. It doesn't do anything else. Barring a SyntaxError, you're not really going to get errors from just defining a function. Please provide your full code, that actually produces (I expect) a NameError. If your full code is long (>10 lines) I highly suggest you simplify your code until it's as simple as possible while reproducing your issue.

I know it defines a function. When I call it with a word it gives the error.
(Jan-07-2020, 10:07 PM)Dixon Wrote: [ -> ]I know it defines a function. When I call it with a word it gives the error.
I copied your function into the Idle shell and called it and it ran perfectly.
def search4vowels(word):
    vowels = set('aeiou')
    found = vowels.intersection(set(word))
    for vowel in found:
        print(vowel)

word = "Hello"
search4vowels(word)
I suggest that your problem is in how or where you are calling it.

Please provide the calling code.
Clunk_Head was quite clear, but I want to make sure you understand: we're not asking for this out of a whim, we want to help you and you haven't helped us enough to do so. We really do want to help, but we can't do so without what we're asking for.
Yes, it works for me too now. I must have called it improperly yesterday. Thanks for the responses guys.
(Jan-06-2020, 11:27 PM)Dixon Wrote: [ -> ]Gives a Traceback name error when trying to invoke: "name 'search4vowels' not defined.

This happens if you call your function, before it's defined.

Example:
foo() # <- NameError!


def foo():
    pass
Error:
NameError: name 'foo' is not defined
This works:
def foo():
    pass


foo() # <- Name exists and points to the memory address, where the function lives

By the way, your code works:

In [2]: def search4vowels(word):
   ...:     vowels = set('aeiou')
   ...:     found = vowels.intersection(set(word))
   ...:     for vowel in found:
   ...:         print(vowel)
   ...:

In [3]: search4vowels('Hello World')
o
e

In [4]: