Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
error
#1
import datetime

def inputYear(msg, errmsg, l, h):
    done = 0
    while (not done):
        try:
            ans = input(msg)
            year = int(ans)
            if (year < l) or (year > h):
                print(errmsg)
            else:
                done = 1
                return year
        except:
            print(errmsg)

def inputMonth(msg, errmsg, l, h):
    done = 0
    while (not done):
        try:
            ans = input(msg)
            month = int(ans)
            if (month < l) or (month > h):
                print(errmsg)
            else:
                done = 1
                return month
        except:
            print(errmsg)
        print(errmsg)

def inputDay(msg, errmsg, year, month, l, h):
    dh = (31,28,31,30,31,30,31,31,30,31,30,31)
    h = dh[month - 1]
    if (year % 4 == 0) and (month == 2):
        h = 29
    done = 0
    while (not done):
        try:
            ans = input(msg)
            day = int(ans)
            if (day < l) or (day > h):
                print(errmsg)
            else:
                done = 1
                return day
        except:
            print(errmsg)

def Lesson1():
    import datetime
    day = '2018-8-6'
    d = datetime.datetime.strptime(day, '%Y-%m-%d')
    wd = ('Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday')
    print('%s is a %s' % (day, wd[d.weekday()]))
def checkWeekday():
    import datetime
    year = inputYear('Please input year(1-9999)', 'Please input a valid year', 1, 9999)
    month = inputMonth('Please input month(1-12)', 'Please input a valid month', 1, 12)
    day = inputDay('Please input day(1-31)', 'Please input a valid day', year, month, 1, 31)
    d = datetime.datetime.strptime(int(year)-int(month)-int(day), '%Y-%m-%d')
    wd = ('Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday')
    print('%s is a %s' % (day, wd[d.weekday()]))

checkWeekday()

year = inputYear('Please input year(1-9999)', 'Please input a valid year', 1, 9999)
print(year)
month = inputMonth('Please input month(1-12)', 'Please input a valid month', 1, 12)
print(month)
day = inputDay('Please input day(1-31)', 'Please input a valid day', year, month, 1, 31)
print(day)
Error:
Traceback (most recent call last): File "C:\Users\LokChi\Desktop\Untitled Project.py", line 65, in <module> checkWeekday() File "C:\Users\LokChi\Desktop\Untitled Project.py", line 61, in checkWeekday d = datetime.datetime.strptime(int(year)-int(month)-int(day), '%Y-%m-%d') TypeError: strptime() argument 1 must be str, not int

any idea on solving this?
Reply
#2
In d = datetime.datetime.strptime(day, '%Y-%m-%d') ( line 53 ) the format for the date is set with zero padded months and deys. But the 'day' variable contains '2018-8-6'. Try to change it to '2018-08-06'. According to the docs.

However, I don't understand why it throws TypeError.


My bad. The error is pointing line 61. Change it to: d = datetime.datetime.strptime(f'{str(year)}-{str(month)}-{str(day)}', '%Y-%m-%d')
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#3
That would be becuase first argument should be a string, not int, as is currently in OPs code (line 61).
Try with this as an argument:
"{}-{}-{}".format(year, month, day)
If you use Python 3.6+ you can also use f-strings instead of format method.
Reply
#4
OP wants to create a datetime.datetime object. Why complicate things with creating string and then convert it to datetime.datetime object?

import datetime
year = 2018
month = 8
day = 6

my_datetime = datetime.datetime(year=year, month=month, day=day)
print(my_datetime.strftime('%A'))
Output:
Monday >>>
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Forum Jump:

User Panel Messages

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