Welcome!
This is the community forum for my apps Pythonista and Editorial.
For individual support questions, you can also send an email. If you have a very short question or just want to say hello — I'm @olemoritz on Twitter.
New coder having trouble with creating class that takes user input date time to date time object
-
New coder developing script for use in my day job. Trying to make a class which take user entered month and year and combines them into date object. I can get the code for date entry and parsing into date object working. but then trying to make into a class so I can used over and over in difference instances which I cant seem to get right. I pasted where I currently am in my trial and error. I have been through many iterations trying to make it work. Any advice would be appreciated
class User_Input_Date(): def __ini__(self, month_input, year_input): month_input = input('What was the month (1-12)') if month_input in ['01', '1', 'Jan', 'January', 'jan', 'january']: month_input = 1 return month_input elif month_input in ['02', '2', 'Feb', 'February', 'feb', 'february']: month_input = 2 return month_input elif month_input in ['03', '3', 'Mar', 'March', 'mar', 'march']: month_input = 3 return month_input elif month_input in ['04', '4', 'Apr', 'April', 'apr', 'april']: month_input = 4 return month_input elif month_input in ['05', '5', 'May', 'may']: month_input = 5 return month_input elif month_input in ['06', '6', 'Jun', 'June', 'jun', 'june']: month_input = 6 return month_input elif month_input in ['07', '7', 'Jul', 'July', 'jul', 'july']: month_input = 7 return month_input elif month_input in ['08', '8', 'Aug', 'August', 'aug', 'august']: month_input = 8 return month_input elif month_input in ['09', '9', 'Sept', 'September', 'sept', 'september']: month_input = 9 return month_input elif month_input in ['10', 'Oct', 'October', 'oct', 'october']: month_input = 10 return month_input elif month_input in ['11', 'Nov', 'November', 'nov', 'november']: month_input = 11 return month_input elif month_input in ['12', 'Dec', 'December', 'dec', 'december']: month_input = 12 return month_input self.year_input = int(input('What was the year?')) self.year_input = year_input return year_input def Combined_User_Input_Date(self, month_input, year_input): combine_date_user_input_month_year = datetime.date(year, month, day=1) return combine_date_user_input_month_year.strftime("%m" + "-" + "%Y") primary_bariatric_date = User_Input_Date() print(primary_bariatric_date.Combined_User_Input_Date())```
-
class User_Input_Date(): def __init__(self): self.month_input = input('What was the month (1-12)') if self.month_input in ['01', '1', 'Jan', 'January', 'jan', 'january']: self.month_input = 1 elif self.month_input in ['02', '2', 'Feb', 'February', 'feb', 'february']: self.month_input = 2 elif self.month_input in ['03', '3', 'Mar', 'March', 'mar', 'march']: self.month_input = 3 else: self.month_input = None self.year_input = int(input('What was the year?')) def printDate(self): print(f'year = {self.year_input} month = {self.month_input}') primary_bariatric_date = User_Input_Date() primary_bariatric_date.printDate()
You store the variables with self.name instead returning it.
-
@danaportenier, see below for a sample of using the built-in parser.
from dateutil.parser import parse month = input('Please give a month: ').strip() year = input('Please give a year: ').strip() datetm = parse(year + ' ' + month) print(datetm.strftime("%m-%Y"))
-
Thanks to all for the help
-
Thanks for all of the responses but still seems to have something wrong with variables in my class. I plan to implement mikael’s parsing tips. Brumm even when I use your code exactly I still get the same error listed below.
Traceback (most recent call last):
File "/private/var/mobile/Library/Mobile Documents/iCloud~com~omz-software~Pythonista3/Documents/test.py", line 64, in <module>
primary_bariatric_date.Combined_User_Input_Date()
File "/private/var/mobile/Library/Mobile Documents/iCloud~com~omz-software~Pythonista3/Documents/test.py", line 59, in Combined_User_Input_Date
self.year_input, self.month_input, day=1)
AttributeError: 'User_Input_Date' object has no attribute 'year_input'Current code:
class User_Input_Date(): def __ini__(self): self.month_input = input('What was the month (1-12)').strip() if self.month_input in ['01', '1', 'Jan', 'January', 'jan', 'january']: self.month_input = 1 elif self.month_input in ['02', '2', 'Feb', 'February', 'feb', 'february']: self.month_input = 2 elif self.month_input in ['03', '3', 'Mar', 'March', 'mar', 'march']: self.month_input = 3 elif self.month_input in ['04', '4', 'Apr', 'April', 'apr', 'april']: self.month_input = 4 elif self.month_input in ['05', '5', 'May', 'may']: self.month_input = 5 elif self.month_input in ['06', '6', 'Jun', 'June', 'jun', 'june']: self.month_input = 6 elif self.month_input in ['07', '7', 'Jul', 'July', 'jul', 'july']: self.month_input = 7 elif self.month_input in ['08', '8', 'Aug', 'August', 'aug', 'august']: self.month_input = 8 elif self.month_input in [ '09', '9', 'Sept', 'September', 'sept', 'september' ]: self.month_input = 9 elif self.month_input in ['10', 'Oct', 'October', 'oct', 'october']: self.month_input = 10 elif self.month_input in ['11', 'Nov', 'November', 'nov', 'november']: self.month_input = 11 elif self.month_input in ['12', 'Dec', 'December', 'dec', 'december']: self.month_input = 12 else: self.month_input = None self.year_input = input('What was the year?').strip() def Combined_User_Input_Date(self): combine_date_user_input_month_year = datetime.date( self.year_input, self.month_input, day=1) return combine_date_user_input_month_year.strftime("%m" + "-" + "%Y") primary_bariatric_date = User_Input_Date() primary_bariatric_date.Combined_User_Input_Date()
-
@danaportenier replace
def __ini__(...)
withdef __init__(...)
(notice the t)Edit: You can also replace your
if
-construct with something like this:inp = input("What was the month (1-12) ").strip() self.month_input = { "01": 1, "1": 1, "Jan": 1, ... "02": 2, "2": 2, "Feb": 2, ... }.get(inp, None) }
This does the following:
- ask the user for input, saving it in the local variable
inp
- create a dictionary (key-value mapping) containing the inputs and their numeric values, then checks if
inp
is in the dictionary. If it is, the value for the input is assigned toself.month_input
, otherwise the valueNone
is used.
I would also recomment to restructure the code as follows:
class UserInputDate(object): # inherit from object, use PEP8-style naming conventions MONTH_TO_YEAR = {...} # your month names to values, as dicussed above def __init__(self, month, year): self.month = self.MONTH_TO_YEAR.get(month, None) # do not call I/O functions from `__init__`. You may later need the same class without input. self.year = year @classmethod def from_input(cls): # use a classmethod to directly create the class from input month = input("Please enther month (1-12)") year = input("Please enther year") return cls(month, year) def combine(self): # as your old code, but using self.month and self.year .... if __name__ == "__main__": # place top-level code inside such an if-statement, so it does not get executed on import primary_bariatric_date = UserInputDate() primary_bariatric_date.combine()
The above code is not tested, but it should prove you with some tips regarding class layout.
Most of these changes seem useless at first, but are more useful later on. - ask the user for input, saving it in the local variable
-
Thanks I’ll give it a try. I have just been learning about class methods so will give it a go