DomantasJ1 DomantasJ1 - 2 months ago 6
Python Question

Syntax error when using return inside a class

Can anyone explain why I am getting a syntax error for the

return
statement in
has_happened()
method.

from sys import argv
import time
from datetime import date


class Game(object):

def __init__(self, home, away, home_odds, away_odds, date):
self.home = home
self.away = away
self.home_odds_str = home_odds
self.away_odds_str = away_odds
self.date = date

def to_string(self):
return self.home + ", " + self.away + ", " + self.home_odds + ", " + self.away_odds + ", " + self.result + ", " + self.date

def has_happened(self):
dates_split = self.date.split("/")
today = date.today()
game_date = date(int(dates_split[2]), int(dates_split[1]), int(dates_split[0])
return str(game_date < today)


game1 = Game("t1", "t2", "7/8", "9/1", "29/9/2016")
game1.has_happened()

Answer
def has_happened(self):
    dates_split = self.date.split("/")
    today = date.today()
    game_date = date(int(dates_split[2]), int(dates_split[1]), int(dates_split[0]))
    return str(game_date < today)