![](/img/trans.png)
[英]My code works fine inside the python shell, but not showing anything when running it on my Visual studio code
[英]Why is my Python code not running correctly in Visual Studio Code, but runs fine in IDLE?
我目前正在學習 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 file in Terminal選項運行它時,我得到的是:
我也嘗試過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'))
這是關於"__main__"
一詞的手冊頁。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.