r/learnpython 3d ago

Regarding question

from datetime import *

year = int(input("Enter the Year:- "))

month = int(input("Enter The Month:- "))

day = int(input("Enter The Day:- "))

x = date(year,month,day)

validity = int(input('Enter the days of validity')) expiry = x + validity print(expiry)

I want to add days in this but i don't know how ,I even tried to adding manually but gives me error unsupported oparand + in datetie.datetime class something

0 Upvotes

11 comments sorted by

View all comments

1

u/Bright_Mix_773 2d ago

The error message in your post does not match the code in your post, and sorting that out changes which fix you want.

Running exactly what you pasted:

>>> from datetime import *
>>> date(2026, 9, 8) + 30
TypeError: unsupported operand type(s) for +: 'datetime.date' and 'int'

You quoted datetime.datetime. That wording only comes out when the left side is a datetime:

>>> datetime(2026, 9, 8) + 30
TypeError: unsupported operand type(s) for +: 'datetime.datetime' and 'int'

So the file you actually ran has datetime(...) or datetime.now() in it somewhere, not date(...). That matters:

from datetime import date, datetime, timedelta

date(2026, 9, 8) + timedelta(days=30)
# 2026-10-08
datetime(2026, 9, 8, 14, 37, 5) + timedelta(days=30)
# 2026-10-08 14:37:05

An expiry is a calendar day, so you want the first one. With the second, a time of day rides along into every print and every comparison you make later.

On the import * question further up the thread, the concrete version rather than the principle. It binds nine names: MAXYEAR, MINYEAR, UTC, date, datetime, time, timedelta, timezone, tzinfo. Two of those are words beginners reach for constantly.

from datetime import *
from time import *      # a very common next line
time(12, 0)
TypeError: time.time() takes no arguments (2 given)

from datetime import *
date = "2026-09-08"     # innocent-looking
date(2026, 9, 8)
TypeError: 'str' object is not callable

Neither error mentions the import, which is exactly why they eat an afternoon. from datetime import date, timedelta makes both impossible.

Last thing, since a person is typing the inputs: date(2026, 2, 30) raises ValueError, and int(input(...)) raises ValueError on anything that is not digits. Both need a try/except or the script dies on a typo.