Nov-29-2018, 06:19 PM
Nov-29-2018, 06:35 PM
The power was within you, all along! https://tvtropes.org/pmwiki/pmwiki.php/M...ouAllAlong
>>> import calendar >>> help(calendar.monthrange) Help on function monthrange in module calendar: monthrange(year, month) Return weekday (0-6 ~ Mon-Sun) and number of days (28-31) for year, month. >>> date = calendar.monthrange(2015, 2) >>> date (6, 28) >>> dir(calendar) ['Calendar', 'EPOCH', 'FRIDAY', 'February', 'HTMLCalendar', 'IllegalMonthError', 'IllegalWeekdayError', 'January', 'LocaleHTMLCalendar', 'LocaleTextCalendar', 'MONDAY', 'SATURDAY', 'SUNDAY', 'THURSDAY', 'TUESDAY', 'TextCalendar', 'WEDNESDAY', '_EPOCH_ORD', '__all__', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', '_colwidth', '_locale', '_localized_day', '_localized_month', '_spacing', 'c', 'calendar', 'datetime', 'day_abbr', 'day_name', 'different_locale', 'error', 'firstweekday', 'format', 'formatstring', 'isleap', 'leapdays', 'main', 'mdays', 'month', 'month_abbr', 'month_name', 'monthcalendar', 'monthlen', 'monthrange', 'nextmonth', 'prcal', 'prevmonth', 'prmonth', 'prweek', 'repeat', 'setfirstweekday', 'sys', 'timegm', 'week', 'weekday', 'weekheader'] >>> calendar.day_name[date[0]] 'Sunday'
Nov-29-2018, 07:34 PM
What's wrong with this... I don't understand...
import calendar y = input("Enter year:") m = input("Enter month:") s = (calendar.monthrange(y, m)[1]) dn = (calendar.day_name[s[0]]) print (f"The last day of entered year and month is {s}, {dn}." )
Nov-29-2018, 08:27 PM
Please don't make me guess. Share the entire error message.
If there isn't an error, show your output, and what you expect the output to be.
If there isn't an error, show your output, and what you expect the output to be.
Nov-29-2018, 08:56 PM
OK, I'm sorry.
import calendar y = input("Enter year:") m = input("Enter month:") s = (calendar.monthrange(y, m)[1]) dn = (calendar.day_name[s[0]]) print (f"The last day of entered year and month is {s}, {dn}." )Here's the error message ( line 95: s = (calendar.monthrange(y, m)[1]) ):
Enter year:2018 Enter month:11 Traceback (most recent call last): File "dn3.py", line 95, in <module> s = (calendar.monthrange(y, m)[1]) File "C:\Users\works\AppData\Local\Programs\Python\Python37-32\lib\calendar.py", line 123, in monthrange if not 1 <= month <= 12: TypeError: '<=' not supported between instances of 'int' and 'str'
Nov-29-2018, 09:04 PM
input()
returns a string, but calendar.monthrange()
is expecting ints, so it can make sure you gave it a valid month. Try this:import calendar year = int(input("Enter year:")) month = int(input("Enter month:")) s = calendar.monthrange(year, month)
Nov-29-2018, 09:30 PM
import calendar year = int(input("Enter year:")) month = int(input("Enter month:")) s = calendar.monthrange(year, month)[1] dn = (calendar.day_name[s[0]]) print (f"The last day of entered year and month is {s}, {dn}." )Error message (line 96: dn = (calendar.day_name[s[0]]) ):
Enter year:2018 Enter month:11 Traceback (most recent call last): File "er.py", line 96, in <module> dn = (calendar.day_name[s[0]]) TypeError: 'int' object is not subscriptable
Nov-29-2018, 10:00 PM
(Nov-29-2018, 09:30 PM)Mof44 Wrote: [ -> ]s = calendar.monthrange(year, month)[1]
The error is telling you exactly what the issue is. The docs for
calendar.monthrange()
state that it returns two ints, one for the day of the week, and another for the number of days in the month. You want both of those values. So why are you ignoring the first?s = calendar.monthrange(year, month)[1] ^^^ why?