Python Forum
If statement variable not defined
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
If statement variable not defined
#4
(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)
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Messages In This Thread
If statement variable not defined - by MTom5 - Sep-03-2018, 01:52 PM
RE: If statement variable not defined - by MTom5 - Sep-03-2018, 02:41 PM
RE: If statement variable not defined - by ichabod801 - Sep-03-2018, 04:31 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
Question Variable not defined even though it is CoderMerv 3 444 Mar-28-2024, 02:13 PM
Last Post: Larz60+
  Variable is not defined error when trying to use my custom function code fnafgamer239 4 690 Nov-23-2023, 02:53 PM
Last Post: rob101
  Printing the variable from defined function jws 7 1,543 Sep-03-2023, 03:22 PM
Last Post: deanhystad
  [variable] is not defined error arises despite variable being defined TheTypicalDoge 4 2,313 Apr-05-2022, 04:55 AM
Last Post: deanhystad
  An IF statement with a List variable dedesssse 3 8,652 Jul-08-2021, 05:58 PM
Last Post: perfringo
  define a variable using an if statement Margaridalopes 2 2,251 Oct-24-2020, 05:47 PM
Last Post: jefsummers
  Function will not return variable that I think is defined Oldman45 6 3,673 Aug-18-2020, 08:50 PM
Last Post: deanhystad
  How to assign a module to a variable even if it's not defined? mandaxyz 5 3,413 Aug-12-2020, 10:34 PM
Last Post: snippsat
  Variable not defined Heyjoe 4 2,647 Jul-10-2020, 11:27 PM
Last Post: Heyjoe
  python library not defined in user defined function johnEmScott 2 3,984 May-30-2020, 04:14 AM
Last Post: DT2000

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020