'Condition has 3 variables in the result: 1 is True and 2 is False. Error "NameError is not defined" for False variables
If a condition is True it returns me 3 results (Victory home, Draw home, Defeat home), while if it is False it returns me 3 results (Victory away, Draw away, Defeat away)
I need to use only 1 variable depending on the case, because the result will be either a Victory or a Draw or a Defeat, which will be identified with the operators <,> or == on the team points. While the other 2 results give me an error (for example if I have a Victory, obviously Draw and Defeat are False). The error is NameError: name 'Draw_home' is not defined
So I thought about using try and except: pass to solve the problem, but it's not the right solution.
WHAT I WANT? I would like to use only 1 of the three variables Victory, Draw or Defeat (depending on whether it is home or away), without getting an error for the other two remaining variables
In order not to lengthen myself with the code, I place only the code useful for solving the problem:
def function():
select_match = partita.get()
team_A = select_partita.split('-')[0]
if team_A:
conn = sqlite3.connect('/...')
cursor = conn.cursor()
cursor.execute("""
SELECT
CASE WHEN team_home = ? THEN 'Last match, played at home'
ELSE 'Last match, played away'
END AS choice,
score_home, score_away, team_home, team_away
FROM Table1
WHERE ? IN (team_home, team_away)
LIMIT 1
""",
(team_A, team_A))
row = cursor.fetchone()
if row:
choice, points_home, points_away, team_home, team_away = row
try:
#If Team A played home, what is the result? Victory home, Draw home, Defeat home?
if choice == 'Last match, played at home':
TeamA_centered_points_if_home = points_home
TeamA_points_conceded_if_home = points_away
opponent_away = team_away
print("Last match, played at home: ", TeamA_centered_points, TeamA_points_conceded, "VS", opponent_away)
Victory_home = TeamA_centered_points_if_home > TeamA_points_conceded_if_home
Draw_home = TeamA_centered_points_if_home == TeamA_points_conceded_if_home
Defeat_home = TeamA_centered_points_if_home < TeamA_points_conceded_if_home
#If Team A played away, what is the result? Victory away, Draw away, Defeat away?
else:
TeamA_centered_points_if_away = points_away
TeamA_points_conceded_if_away = points_home
opponent_home = team_home
print("Last match, played away: ", TeamA_centered_points_if_away, TeamA_points_conceded_if_away, "VS", opponent_home)
Victory_away = TeamA_centered_points_if_away > TeamA_points_conceded_if_away
Draw_away = TeamA_centered_points_if_away == TeamA_points_conceded_if_away
Defeat_away = TeamA_centered_points_if_away < TeamA_points_conceded_if_away
except:
pass
...same exact condition, but which does not concern the last match, but concerns the LAST 2 MATCHES
...same exact condition, but which does not concern the last match, but concerns the LAST 3 MATCHES
...same exact condition, but which does not concern the last match, but concerns the LAST 4 MATCHES
Solution 1:[1]
Maybe you can have a single variable called result_away that holds the result. Then use an if statement to set it:
if TeamA_centered_points_if_away > TeamA_points_conceded_if_away:
result_away = 'VICTORY'
elif TeamA_centered_points_if_away == TeamA_points_conceded_if_away:
result_away = 'DRAW'
else:
result_away = 'DEFEAT'
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Code-Apprentice |
