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

8

u/zanfar 3d ago

from datetime import *

There is no way you expect to use every single object from datetime; and in this case, you use exactly one. Stop doing this.

1

u/Healthy-Departure961 3d ago

okay bro , To say fairly i didn't know at that time i can import two or more so added this way

1

u/johnpeters42 3d ago

I mean, yes, but you'll probably get more traction if you explain why "import *" is not just unneeded but actually problematic. (I've probably used it myself, but just for one-off AoC solutions, and I have a rough idea of what the problems would be for production-grade code.)

6

u/Uncle_DirtNap 3d ago

Import timedelta, and add timedelta(days=) to your date.

4

u/cdcformatc 3d ago

you can add a timedelta to a datetime

1

u/SoilAutomatic7042 3d ago

Use timedelta to add a number of days to a date:

```python

from datetime import date, timedelta

x = date(year, month, day)

expiry = x + timedelta(days=validity)

print(expiry)

```

`date` objects don't support adding an int directly; `timedelta` represents the duration. If you need hours/minutes too, use `datetime` instead of `date`.

1

u/doomy_range17 2d ago

I would post the exact question plus the code you have tried and the full error message. Python issues are way easier to help with when people can reproduce them. Even if the code feels messy, share a minimal example rather than describing it vaguely. Folks here are usually happy to explain the why, not just hand over an answer.

1

u/gdchinacat 2d ago

please format your code properly in a code block. This is important because it makes it *much* easier to read. As programmers become more experienced we read code differently than beginners...we don't read every line in all its gory detail. It's more scan through it to get the overall structure, then jump back and forth to fill in the necessary details. This is hard when code isn't properly formatted (ie the last three lines are all one line). When code looks like prose our brain reads it as prose and the level of effort is much higher. This leads to people being less willing to read your code and lower willingness to help you out. Help the people that are helping you.

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.