Python Forum

Full Version: If statement variable not defined
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I cannot get this bit of code working

if ({Status} == "Y"):
    Online_Status = 1
elif ({Status} == "N"):
    Online_Status = 0
Get error saying Online_Status not defined but i have defined it in the if statement i am guessing this isnt syntactically correct but i cant figure it out tried a few different things to no avail.

import datetime

#UserName Variable
UserName = "UserName"
#Password Variables
Online_Pass = "PasswordOnline"
Offline_Jan = "Password1"
Offline_Feb = "Password2"
Offline_Mar = "Password3"
Offline_Apr = "Password4"
Offline_May = "Password5"
Offline_Jun = "Password6"
Offline_Jul = "Password7"
Offline_Aug = "Password8"
Offline_Sep = "Password9"
Offline_Oct = "Password10"
Offline_Nov = "Password11"
Offline_Dec = "Password12"

dt = datetime.datetime.now()

print("Date : ", dt.day, "/", dt.month, "/", dt.year )

Status = input("Online? (Y/N) ")

if ({Status} == "Y"):
    Online_Status = 1
elif ({Status} == "N"):
    Online_Status = 0

if (dt.month == 1):
    print("Month = January")
    if (Online_Status == 1):
        print(Online_Pass)
    elif (Online_Status == 0):
        print(Offline_Jan)
elif (dt.month == 2):
    print("Month = Febuary")
    if (Online_Status == 1):
        print(Online_Pass)
    elif (Online_Status == 0):
        print(Offline_Feb)
elif (dt.month == 3):
    print("Month = March")
    if (Online_Status == 1):
        print(Online_Pass)
    elif (Online_Status == 0):
        print(Offline_Mar)
elif (dt.month == 4):
    print("Month = April")
    if (Online_Status == 1):
        print(Online_Pass)
    elif (Online_Status == 0):
        print(Offline_Apr)
elif (dt.month == 5):
    print("Month = May")
    if (Online_Status == 1):
        print(Online_Pass)
    elif (Online_Status == 0):
        print(Offline_May)
elif (dt.month == 6):
    print("Month = June")
    if (Online_Status == 1):
        print(Online_Pass)
    elif (Online_Status == 0):
        print(Offline_Jun)
elif (dt.month == 7):
    print("Month = July")
    if (Online_Status == 1):
        print(Online_Pass)
    elif (Online_Status == 0):
        print(Offline_Jul)
elif (dt.month == 8):
    print("Month = August")
    if (Online_Status == 1):
        print(Online_Pass)
    elif (Online_Status == 0):
        print(Offline_Aug)
elif (dt.month == 9):
    print("Month = September")
    if (Online_Status == 1):
        print(Online_Pass)
    elif (Online_Status == 0):
        print(Offline_Sep)
elif (dt.month == 10):
    print("Month = October")
    if (Online_Status == 1):
        print(Online_Pass)
    elif (Online_Status == 0):
        print(Offline_Oct)
elif (dt.month == 11):
    print("Month = Novemeber")
    if (Online_Status == 1):
        print(Online_Pass)
    elif (Online_Status == 0):
        print(Offline_Nov)
elif (dt.month == 12):
    print("Month = December")
    if (Online_Status == 1):
        print(Online_Pass)
    elif (Online_Status == 0):
        print(Offline_Dec)
Neither your if nor your elif trigger, so you never actually define Online_Status, and when you start checking the month, that's when you get the error.

The problem is {Status}. That makes a set containing Status, which is not equal to Status. Get rid of the curly braces on lines 26 and 28, and see how that works.
(Sep-03-2018, 02:24 PM)ichabod801 Wrote: [ -> ]Neither your if nor your elif trigger, so you never actually define Online_Status, and when you start checking the month, that's when you get the error.

The problem is {Status}. That makes a set containing Status, which is not equal to Status. Get rid of the curly braces on lines 26 and 28, and see how that works.

This worked thanks

Is there a way i can condense the if/elif month statements ?
(Sep-03-2018, 02:41 PM)MTom5 Wrote: [ -> ]Is there a way i can condense the if/elif month statements ?

Dictionaries:

month_data = {1: {'name': 'January', 'offline': 'Password1'},
    2: {'name': 'February', 'offline': 'Password2'},
    ...
    12: {'name': 'December', 'offline': 'Password12'}}
...
print('Month =', month_data[dt.month]['name'])
if Online_Status == 1:      # You don't need the parentheses.
    print(Online_Pass)
elif Online_Status == 0:
    print(month_data[dt.month]['offline'])
Actually, since it's indexed to integers, you could do a list of tuples:

month_data = [('', ''), ('January', 'Password1'), ('February', 'Password2'), ..., 
    ('December', 'Password3')]
...
name, offline = month_data[dt.month]
print('Month =', name)
if Online_Status:     # You don't even need the == 1.
    print(Online_Pass)
else:
    print(offline)