变量赋值问题(Python)

时间:2017-10-01 17:58:09

标签: python python-3.x

def bet(sport,home,away,market,selection,odds,stake,profit,bookie,tipster=None):
    global df
    if profit > 0:
        is_won = True
    elif profit < 0:
        is_won = False
    elif profit == 0:
        None
    temp_df = pd.DataFrame([[sport,home,away,market,selection,odds,tipster,stake,is_won,profit,bookie]], columns=columns_list)
    df = df.append(temp_df, ignore_index=True)
    if profit > 0:
        print("Good bet on %s! :)" %selection)
    elif profit < 0:
        if tipster is not None:
            print("Don't trust %s! :(" %tipster)
        elif tipster is None:
            print("Shame...")
    elif profit == 0:
        print("Cowboys")

这是返回UnboundLocalError:局部变量&#39; is_won&#39;在分配前引用

1 个答案:

答案 0 :(得分:0)

问题出在profit == 0时。在这种情况下,将不会定义is_won。此外,你的缩进搞砸了。