我試圖創建一個函數,我可以定時調用以檢查良好的ping並返回結果,以便我可以更新屏幕顯示。我是python的新手,所以我不完全理解如何返回值或在函數中設置變量。Python函數來測試ping
這裏是我的代碼工作:
import os
hostname = "google.com"
response = os.system("ping -c 1 " + hostname)
if response == 0:
pingstatus = "Network Active"
else:
pingstatus = "Network Error"
這是我在創造一個功能的嘗試:
def check_ping():
hostname = "google.com"
response = os.system("ping -c 1 " + hostname)
# and then check the response...
if response == 0:
pingstatus = "Network Active"
else:
pingstatus = "Network Error"
,這裏是我如何顯示pingstatus
:
label = font_status.render("%s" % pingstatus, 1, (0,0,0))
所以我正在尋找的是如何從函數返回pingstatus。任何幫助將不勝感激。
...'return pingstatus'? – jonrsharpe 2014-10-20 14:54:46
要學習如何調用函數和返回值,我推薦[Python教程](https://docs.python.org/3/tutorial/controlflow.html#defining-functions)。 – Kevin 2014-10-20 14:55:19