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!)
Related
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 8 months ago.
Improve this question
I'm building a scraping spider and I would like some help on how to extract the right information out of each response in Python
response.css(".print-acta-temp::text").get()
'TEMPORADA 2021-2022'
I would like to know how to collect only the 2021-2022. Should I use the str command?
response.css(".print-acta-data::text").get()
'Data: 14-05-2022, 19:00h'
I need to extract only the date into one variable and the time into another variable.
response.css(".print-acta-comp::text").get()
' CADET PRIMERA DIVISIÓ - GRUP 2'
I need to collect the data before the first space, the data collected between the 2 spaces and finally the number into another variable.
response.css(".print-acta-jornada::text").get()
'Jornada 28'
I need to collect the data after the first space.
if you trust the website to produce the data you want exactly followed by 'TEMPORADA ' all the time you can use
tu_string = 'TEMPORADA 2021-2022'
nueva_string = tu_string.replace('TEMPORADA ','')
print (nueva_string)
like, there's regex and all of that, but you can worry about learning that later, tbh.
I need to collect the data before the first space, the data collected
between the 2 spaces and finally the number into another variable.
a simple way to do this is to split
teva_string = 'CADET PRIMERA DIVISIÓ - GRUP 2'
teva_lista = teva_string.split(' ')
print (teva_lista)
Any decision on how to parse a string is going to depend on one's assumptions about what form the strings are going to take. In the particular case of 'TEMPORADA 2021-2022', doing my_string.split(' ')[1] will get the years. 'Data: 14-05-2022, 19:00h'.split(' ') will get the list ['Data: 14-05-2022,, '19:00h'], while 'Data: 14-05-2022, 19:00h'.split('-') will get ['Data: 14-05-2022', ' 19:00h']. You can also use datetime libraries or regular expressions, with the latter allowing for more customization if the form of your data varies.
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 1 year ago.
Improve this question
I am writing software in Python that needs to do the following:
Assign values to variables by getting input from the user
Choose the next question to ask the user based on their previous answer
Take action based on the variables once all questions have been asked
I saw an example on here which set questions in a Python dictionary, and that included what question to jump to based on the answer entered. This was very neat and easy to follow, so I started implementing that. I was then going to follow this by comparing the variables to a second dictionary which would then determine what action to take - a decision matrix I believe this is called.
I came to a grinding halt, however, on a question that asks for the date. I have no idea how to check the date is in the right format, and how to get Python to check a valid date has been entered and, if it has, to move to a specific next question. Turns out there is more than one occurrence of this problem.
The code follows below. You can easily see in questions relating to candate and frequency1 and frequency2 where I have come a cropper.
The question, I guess, is whether I can do this using a dictionary to define questions and flow, or whether I will have to suck it up and create a load of IF statements. The only reason I started with dictionaries is to make it easy to implement, maintain and expand. Once I have nailed this, I need to implement much bigger question sets, so would like the question flow to be as elegant and simple as possible.
Apologies for obfuscating the questions.
If you answer, I would just point out this is the first thing I have tried to write in Python beyond asking for someone's name and printing it so simpler is better for me! Thanks!
#Stopquestions is a dictionary containing all the questions that could be asked of the user. It also controls the flow of the questions.
stopquestions = {
'stopconfirm':
{
'prompt':'Do you want to take this action? Y/N ',
'Y':'prev',
'N':'genericerror'
},
'prev':
{
'prompt':'Previously done? Y/N ',
'Y':'bamerch',
'N':'frequency1'
},
'bamerch':
{
'prompt':'Was that with B or M? B/M ',
'B':'candate',
'M':'candate'
},
'candate':
{
'prompt':'What was the date? DD/MM/YYYY ',
'DD/MM/YYYY':'length',
'Anything but the above':'genericerror'
},
'length':
{
'prompt':'Yes or no with reference to length? Y/N ',
'Y':'frequency2',
'N':'frequency2'
},
'frequency1':
{
'prompt':'Choose freq? W/F/M/Q/A ',
'W/F/M/Q/A':'dis',
'Anything but the above':'genericerror'
},
'frequency2':
{
'prompt':'Choose freq? W/F/M/Q/A ',
'W/F/M/Q/A':'end',
'Anything but the above':'genericerror'
},
'dis':
{
'prompt':'Disagreement with recent? Y/N ',
'Y':'end',
'N':'end'
},
'genericerror':
{
'text':'This is a basic example of how we can catch errors.',
'action':'end'
}
}
I would look at the date, and if it does not match the formatting ask the user to reinput the data.
arrow is a great tool to handle this:
import arrow
try:
arrow.get('01/01/2021', 'MM/DD/YYYY')
except arrow.parser.ParserMatchError:
print(False)
https://arrow.readthedocs.io/en/latest/
UPDATE
def check_format(date, fmt='MM/DD/YYYY'):
try:
x = arrow.get(date)
return x.format('MM/DD/YYYY')
except (arrow.parser.ParserMatchError, arrow.parser.ParserError):
if fmt: # 'MM/DD/YYYY'
try:
x = arrow.get(date, fmt)
return x.format('MM/DD/YYYY')
except arrow.parser.ParserMatchError:
return False
return False
print(check_format('2021-01-01')) # 01/01/2021
print(check_format('2021-01-01', 'MM/DD/YYYY')) # 01/01/2021
print(check_format('01/01/2021', 'MM/DD/YYYY')) # 01/01/2021
print(check_format('June was born in May 10 1980', 'MMMM D YYYY'))) # 05/10/1980
print(check_format('01/2021', 'MM/DD/YYYY')) # False
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 2 years ago.
Improve this question
I have a piece of code that is a storyline but I want to put it in a certain order.
Outside = ["You look around for something to help you", "You remember the toolbox in the back of the plane", "You go to the back of the plane, and open the stairwell", "You open the back and see the toolbox"]
for Outside in Outside:
print(Outside)
time.sleep(3)
#Outside------------------------------------------------------
Inside = ["you sit inside this plane all alone", "A gentle breeze rustles the wheat around you"]
for Inside in Inside:
print(Inside)
time.sleep(3)
#Inside-------------------------------------------------------
Landing = ["Landing gear out", "Touchdown"]
Exitplane = input("\nDo you want to exit the plane: ")
if Exitplane == ("y"):
print("You hop onto the ground" + Outside)
if Exitplane == ("n"):
print(Inside)
#Endcode-------------------------------
As you can see above that is the code I want to run but I want to run the Landing section first then run either Inside or outside (depending on user input)
Thank you for your time
Create a list with the str-values you have created:
story = [Outside, Inside, Landing]
Then you can print the values, as their values are stored in order in the list. If you have two sets of strings in each part of the story this should work:
for i, j story:
print(i, j)
I'm not sure as to how to print the different parts of the stories if they have different numbers of strings. This is because the "i, j" part of the for-loop prints the first two string of the list of strings. You could drop the commas between the different sentences in "Outside, Inside and Landing", whereas you could simply print it normally:
for i in list:
print(i)
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.
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