简体   繁体   中英

Why is my Python code not running correctly in Visual Studio Code, but runs fine in IDLE?

I am currently learning Python and have come across a very strange problem (to me at least). When I type this code:

def search4vowels(word):
    """Display any vowels found in a supplied word."""
    vowels = set('aeiou')
    found = vowels.intersection(set(word))
    return bool(found)

in an IDLE edit window and run it using IDLE, it all works fine and I need to type my function in at the prompt in order for it to start.

However, when I write the same code in Visual Studio Code and run it using the Run Python file in Terminal option all I get is:

IDE截图

I have also tried the Code Runner extension and that gives me a blank output page and no way to type in the function.

For example : search4vowels('galaxy')

And the output that I expect should be: True (Because a vowel would be present in the word, therefore True)

However, nothing happens.

You are defining function search4vowels() , but Visual Studio Code doesn't know how to run it.

Try adding this in your code:

def search4vowels(word):
    """Display any vowels found in a supplied word."""
    vowels = set('aeiou')
    found = vowels.intersection(set(word))
    return bool(found)

# This tells your code to run the function search4vowels():
if __name__ == '__main__':
    print(search4vowels('your word'))

Here is the manual page about the word "__main__" .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM