gpt4 book ai didi

python - 为什么我的 Python 代码在 Visual Studio Code 中无法正确运行,但在 IDLE 中运行良好?

转载 作者:行者123 更新时间:2023-11-30 22:12:05 25 4
gpt4 key购买 nike

我目前正在学习Python,遇到了一个非常奇怪的问题(至少对我来说)。当我输入此代码时:

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

在 IDLE 编辑窗口中并使用 IDLE 运行它,一切正常,我需要在提示符处输入我的函数才能启动。

但是,当我在 Visual Studio Code 中编写相同的代码并使用 Run 运行它时终端中的 Python 文件 选项我得到的是:

IDE screenshot

我还尝试了Code Runner扩展,它给了我一个空白的输出页面,并且无法输入函数。

例如:search4vowels('galaxy')

我期望的输出应该是:True(因为单词中会出现元音,因此 True)

但是,什么也没发生。

最佳答案

您正在定义函数 search4vowels(),但 Visual Studio 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'))

这里是the manual page关于单词“__main__”

关于python - 为什么我的 Python 代码在 Visual Studio Code 中无法正确运行,但在 IDLE 中运行良好?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51230513/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com