I don't know what to do next [closed] - python

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I am making a program to tell you the day of the week when told the date.
This is what I have got so far but I don't know how to continue.
print('Welcome to the Daygram, the program that tells you the day on any date.')
print('Enter the date in the following format: DD MM YYYY')
date_input = input(What is the date? (DD MM YYYY)')
date_array = date_input.split(' ')
date = date_array[0]
month = date_array[1]
year = date_array[2]
If anyone could help me so I could figure out what I need to do next.

I'll help you with some parts, but most of this code should be a learning experience for you.
import datetime
# prompt here for the date
# put it into a datetime.datetime object named "day"
# this is the part of the code you need to type
day_array = ["Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"]
day_of_week = day_array[day.weekday()]
The datetime module should be your go-to when working with dates. It gives you a comprehensive object with a list of methods relating to time and date, which is EXACTLY what you're looking for. You initialize a datetime object using datetime.datetime(YEAR,MONTH,DAY) (where you fill in the year, month, and day). You can also set a time, but it's not necessary in your use case.
The datetime object has a method called weekday that returns a number (0 through 6) that represents which day of the week that day represents. I built an array day_array that maps each day to the index it represents (e.g., a Monday will return 0, so day_array[0] == "Monday" and etc).
At this point, it's child's play. The hard part is going to be to prompt the user for a day and convert that into a datetime object. That I'll leave for you.

Related

finding friends' birthdays in python [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I have found help with starting an assignment that I previously did not know how to begin. I currently need to figure out a way to compare several birthdays with the current date, as prompted by the user, with the last day of a month as prompted by the user. I know there is a date timemodule that can tell the current date but I am unsure of how to use the module to work with what the user inputs...if that makes sense.
For example: If the user inputs 11/27/18 as the current date and June as the comparison month (my professor requires a month be entered instead of a date), I need to compare the birthdays from an opened file with 11/27/18 and 06/30/19 and print however many of those dates occur before July 1st, 2019 (it needs to be printed in words, my professor requires specific formatting).
I know how to format the dates that are input in my program, but I am unsure how to compare the dates from the file with what is input for comparison as it could be different every time. Currently what I have now is the inputs of strings where the user would prompt the current date and the month and I have the file opened.
your program would look something like this its not exactly what your criteria is but this is a step in the right direction. This is just one way to do it so someone might have a better program.
FriendList=[" name 1", "name 2"]
PhoneList=[ " 123" , "456"]
Birthday=["January" , "February"]
print(Birthday)
Date=input(" please choose a month or press q to exit")
# in lists the first string or number in each list will be together
# example: if janurary is chosen then name 1 and 123 will appear
while Date != 'q': # runs until q is pressed to quit
for i in range (2):# loop goes through the 2 names in the list
if Birthday[i] == Date: # compares the list of names to the date
print(FriendList[i],"" , Date[i], "" ,PhoneList[i])
Date=input(" please choose a month or press q to exit")
First you need to check the dates, start by figuring out this part: "birthday between the current date and the last day of a month "
Once you have that working, loop through the friends, and if they meet the condition then add them to a new list.
Sort the new list by alphabetical order, and print it.
This should give you an idea of where to start, once you've some code you can post that and ask for help for specific problems, but nobody will do your final for you (that's cheating!)

Organizing date format (non-Gregorian calendar) - Python 3

I have a large sales database the first column of which is the purchase date. The problem is some of these dates are entered in DD.MM.YY format, some in YY.MM.DD and some in YYYY/MM/DD. I want to make them all to same format. What is the cleanest way I can do this?
Note 1: I'm thinking of doing a series of ifs but that would be a lot of conditions so I'm wondering if there is a cleaner shortcut.
Note 2: An additional complication is that the dates are in Jalaali calender and not Gregorian. I have the function that will convert them to gregorian but I need to pass the correct year, month, day arguments to it; this is why I want to bring them all to a single format. But additionally, this means that if you offer some "Gregorian-only" solutions, like dateutil.parser, it might not work.
Immediately after posting this I found/thought of a solution myself, but instead of deleting the question I decided to post the answer in case someone else come to a similar problem.
tl;dr - I just added a century option to dateutil.parser. I didnt know how to but I found this.
Here's my end code:
from khayyam import JalaliDate
from dateutil.parser import parse, parserinfo
class MyParserInfo(parserinfo):
def convertyear(self, year, *args, **kwargs):
if year < 100:
year += 1300
return year
if __name__ == '__main__':
dt = parse("9.12.96", MyParserInfo()).date()
a=JalaliDate(dt.year, dt.month, dt.day).todate()
print(dt)
print(a)
#1396-09-12
#2017-12-03

Using Python to get current time in yyyy-mm-ddThh:mm:ss.000Z format and also add days [duplicate]

This question already has answers here:
How to add hours to current time in python
(4 answers)
Closed 5 years ago.
I'm using Python and I need to get the time in a certain format that this API uses. So I need to get current time in the format of yyyy-mm-ddThh:mm:ss.000Z and then also create a new date in the future by adding a number of days to it.
So if I could get the current time of 2017-08-19T07:00:00.000Z and add 30 days to it to get 2017-09-18T07:00:00.000Z. What would be the easiest way to write this in Python?
Current date
import datetime
now = datetime.datetime.now()
format_iso_now = now.isoformat()
Add 10 days
from datetime import timedelta
later = now + timedelta(days=10)
format_later_iso = later.isoformat()
Output
print(format_iso_now, format_later_iso)
2017-08-20T02:43:07.177167 2017-08-30T02:43:07.177167
And to match your needs
print(now.strftime('%Y-%m-%d %H:%M:%S:%fZ'))
2017-08-20 02:48:00:103856Z
More details on documentation

Disable Program at specific date [duplicate]

This question already has answers here:
Checking date against date range in Python
(5 answers)
Closed 8 years ago.
So I would like to write a program that disables itself on let's say May 1st.
How would I do that? I was thinking about getting the time with localtime and then writing a "if block" to ask the program if the date is >= to the May 1st. If so it should prompt the user that he can not use the program any longer cause it expired.
But I have a problem writing the if statement, since localtime returns so many values. How would I write the if block?
What you are looking for is to convert datetime to date which you can do by datetime.datetime.now().date(), for the if condtion you can do something like this:
THis is just an example:
import datetime
if datetime.datetime.now().date() >= datetime.date(2012, 1, 15): #insert your date here
print "True"
...........
Your Code
...........
All the Documentation you would ever want on this topic is located here, http://docs.python.org/2/library/datetime.html

Looking for *existing* Python module for determining fiscal months

I'm in calendar hell, and I'm hoping there exists a Python module out there that does what I want.
I'm making a Python web app that deals with subscriptions. It's conceptually similar to a cell phone plan: You start your subscription on a certain date (say 1.13.2011), and for every billing month you have a bunch of "sessions" (phone calls), that you would be charged for.
We need to:
Know under which billing month each session falls.
Know the start time and end time of each billing month.
For example, if you signed up on 1.13.2011, and made a phone call on 1.20.2011, it would count on your first billing month. Same for a phone call on 2.10.2011. But if you were to make a phone call on 2.15.2011, it will count on your second billing month.
Regarding start and end dates: If today is 2.15.2011, then the start date of the current month is 2.13.2011 and its end date is 3.13.2011.
You may be thinking this is not so complicated, but then you have to consider that months have different lengths. The rule for handling this is that if your subscription started on the 30th of whatever month, its cutoff dates on each month would be min(30, n_days_in_that_month). This goes for 29, 30 and 31 as well.
I tried coding this, but it got too complex. What I'm looking for is a ready-made, existing module that does these things.
For the love of God don't post an answer with a sketch of an implementation! This is useless for me. I appreciate your intentions, but in calendar hell, sketches of implementations do not help. I already have a sketch of an implementation, and debugging yours will take just as long as debugging mine.
I am only interested in using an existing module that handles such calendar tasks. Do you know one?
http://labix.org/python-dateutil
Ram's edit: The dateutil.rrule.rrule class is the one that did exactly what I wanted.
Regarding start and end dates: If today is 2.15.2011, then the start date of the current month is 2.13.2011 and its end date is 3.13.2011.
You may be thinking this is not so complicated, but then you have to consider that months have different lengths. The rule for handling this is that if your subscription started on the 30th of whatever month, its cutoff dates on each month would be min(30, n_days_in_that_month). This goes for 29, 30 and 31 as well.
Its still pretty basic. Use datetime module to store datetimes, so you can easily parse out the day (e.g., if dt is a date then dt.day). A billing cycle starts on say the 29th (toughest type of case). Let billing_cycle_day=29. A billable event occurs on say the event_day=10, event_month=5. Then since event_day < billing_cycle_day you bill to event_month's bill. Otherwise you bill to the next months bill (remembering that if month=12; you have to increment the year).
So now the billing cycle will always be from the 29th to the 28th in the next month. The complication arises if say a date like 2/29/2011 doesn't exist. E.g., a billing cycle start_date should be 2/29/2011 (but it doesn't exist); in this case you just make it the first on the next month.
billing_cycle_day = 29
year, month = 2011, 2
import datetime
def create_date_or_first_of_next_month(year, month, day):
try:
return datetime.date(year, month, day)
except ValueError:
year_n, month_n = (year, month+1) if month != 12 else (year+1, 1)
return datetime.date(year_n, month_n, 1)
This problem is not as hard as you think. All you have to do is write a function that given a starting day (like 13 or 30) it returns two date objects which are the beginning and end of the current fiscal month. You have already sketched out all the details in your question. Best to include an optional todayis parameter to the function so that you specify what day to use as a reference for today. For instance, if today is the 15th of October 2011, and you specify 13, the function would assume that you mean the 13th of October 2011. But if you want to rerun June data, you would specify todayis=date(2011,06,13)
The return values (start and end) allow you to pinpoint dates that belong in this fiscal month. But if the date is before the start date and less than 29 days before the start date, then you can also pinpoint in the previous fiscal month. The same goes for the next fiscal month. This is useful because there will be a lot of situations where you process data after a few days, so you will have a mix of two fiscal months to process.

Categories