Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
def search_for_vowels():
#1
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.
Reply
#2
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.
Reply
#3
(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.
Reply
#4
(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.
Reply
#5
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.
Reply
#6
Yes, it works for me too now. I must have called it improperly yesterday. Thanks for the responses guys.
Reply
#7
(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]:
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020