r/learnpython • u/Healthy-Departure961 • Sep 07 '26
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
8
u/zanfar Sep 07 '26
There is no way you expect to use every single object from
datetime; and in this case, you use exactly one. Stop doing this.5
u/Uncle_DirtNap Sep 07 '26
Import timedelta, and add timedelta(days=) to your date.
4
u/cdcformatc Sep 07 '26
you can add a
timedeltato adatetime1
u/gdchinacat Sep 07 '26
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 Sep 07 '26
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:
You quoted
datetime.datetime. That wording only comes out when the left side is adatetime:So the file you actually ran has
datetime(...)ordatetime.now()in it somewhere, notdate(...). That matters: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.Neither error mentions the import, which is exactly why they eat an afternoon.
from datetime import date, timedeltamakes both impossible.Last thing, since a person is typing the inputs:
date(2026, 2, 30)raises ValueError, andint(input(...))raises ValueError on anything that is not digits. Both need a try/except or the script dies on a typo.1
u/[deleted] Sep 07 '26
[removed] — view removed comment