Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
name of the day
#1
import calendar
year = 2015
month = 2
print(calendar.monthrange(year, month)[1])
The terminal:
28

What I need:
Saturday,28.

Thanks in advance for help.
Reply
#2
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'
Reply
#3
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}." )
Reply
#4
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.
Reply
#5
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'
Reply
#6
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)
Reply
#7
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
Reply
#8
(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?
Reply


Forum Jump:

User Panel Messages

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