获取按计划运行的函数的返回值

时间:2017-11-08 00:30:26

标签: python return scheduled-tasks schedule

所以我有一个功能,检查我最喜欢的运动队是否在那天比赛:

parent.findViewById

我是新手程序员,所以请忽略任何"良好做法"或者"更有效的方式"我失踪了!

如果多伦多在某一天上场比赛,则会返回True,如果没有,则返回False。我想要返回这个值,所以我可以在代码后面使用它的结果,虽然我无法弄清楚如何获得预定事件的返回。

我使用以下代码设置了计划:

def checkGameDay():

today = str(datetime.date.today())
yyyymmdd = today.replace('-', '')
#yyyymmdd = '20171104' #test date that toronto played on (lost 6-4 to stL)
url = 'http://scores.nbcsports.msnbc.com/ticker/data/gamesMSNBC.js.asp?sport=%s&period=%s'
r = requests.get(url % ('NHL', yyyymmdd))
json_parsed = r.json()
check = 0

for game_str in json_parsed.get('games', []):
    game_tree = ElementTree.XML(game_str)
    away_tree = game_tree.find('visiting-team')
    home_tree = game_tree.find('home-team')
    gamestate_tree = game_tree.find('gamestate')
    home_nickname = home_tree.get('nickname')
    away_nickname = away_tree.get('nickname')
    home_alias = home_tree.get('alias')
    away_alias = away_tree.get('alias')
    away_score = away_tree.get('score')
    home_score = home_tree.get('score')
    x = 0;
    gamesToday = len(json_parsed.get('games', []))

    if away_alias == 'Tor' or home_alias == 'Tor':
        checkGoal()
        print "Toronto is playing today"
        return True         

    else:
        check = check + 1

    if check == gamesToday:
        print "Toronto does not play today" 
        return False

我知道应该触发返回,因为打印效果非常好!

有人知道如何创建一个接受此返回值的变量吗?非常感谢!我很坚持这个......

1 个答案:

答案 0 :(得分:0)

尝试这样的事情:

 bool_value = False

def test_function():
    global bool_value
    #bool_value = False

    for i in range(10):
        if i%2 == 1:
            bool_value = True
        else:
            bool_value = False

    #return bool_value

storedVariable = bool_value

不要从函数返回任何值。全局定义变量并更新它以供以后使用。

希望这会有所帮助。