我试图创建一个函数,我可以定时调用以检查良好的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