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 last year.
Improve this question
could u please help, it least the beginning, where i need to start.
the task is
Enter the month number (1-12) of your date of birth: 7 (for example)
Enter the number of your date of birth: 4
Your zodiac sign: Aries
zodiac = {
"aries": '21.03-20.04',
"taurus": '21.04-21.05',
"gemini": '22.05-21.06',
"cancer": '22.06-22.07',
"lio": '23.07-22.08',
"virgo": '23.08-23.09',
"libra": '24.09-23.10',
"scorpio": '24.10-22.11',
"sagittarius": '23.11-21.12',
"capricorn": '22.12-20.01',
"aquarius": '21.01-18.02',
"pisces": '19.02-20.03',
}
To check the validity of the date, i will also need another dictionary, with a max. number of days per month
i did this task with if/elif - its ok for me, but using dict....a little problem
I've put the max number of days per month as another dictionary. One way you can achieve this:
zodiac = {
"aries": '21.03-20.04',
"taurus": '21.04-21.05',
"gemini": '22.05-21.06',
"cancer": '22.06-22.07',
"lio": '23.07-22.08',
"virgo": '23.08-23.09',
"libra": '24.09-23.10',
"scorpio": '24.10-22.11',
"sagittarius": '23.11-21.12',
"capricorn": '22.12-20.01',
"aquarius": '21.01-18.02',
"pisces": '19.02-20.03',
}
month_days = {
1 : 31,
2 : 28,
3 : 31,
4 : 30,
5:31,
6:30,
7:31,
8:31,
9:30,
10:31,
11:30,
12:31
}
month = input("Enter the month number (1-12) of your date of birth:")
day = input("Enter the number of your date of birth:")
def convert_to_days(month, d):
days = 0
for i in range(1,int(month)):
days += d[i]
return days
user_zodiac = ""
for item in zodiac.items():
limits = item[1].split("-")
day_and_month = [limit.split(".") for limit in limits]
if (int(day_and_month[0][0].lstrip("0"))+convert_to_days(int(day_and_month[0][1].lstrip("0")), month_days)) \
<= int(day) + convert_to_days(month, month_days) <= \
(int(day_and_month[1][0].lstrip("0"))+convert_to_days(int(day_and_month[1][1].lstrip("0")), month_days)):
user_zodiac = item[0]
break
if user_zodiac == "":
user_zodiac = "capricorn"
print(f"Your zodiac sign is {user_zodiac}")
Essentially what we do is convert the months into days (i.e. maximum of 365 for 31.12) and check if the entered month and day falls between certain ranges.
Note that since you are entering in month number first and date second, a person with a birthday on July 4th has the sign of Cancer, not Aries, which would be if you had a birthday on April 7th.
Output:
Enter the month number (1-12) of your date of birth:7
Enter the number of your date of birth:4
Your zodiac sign is cancer
Enter the month number (1-12) of your date of birth:11
Enter the number of your date of birth:22
Your zodiac sign is scorpio
Enter the month number (1-12) of your date of birth:2
Enter the number of your date of birth:14
Your zodiac sign is aquarius
Related
A Pension in simplest terms is an amount of money paid to an employee agreed upon from an employer based on how long they worked.
The code I have below is the code based off input data (Year Start Employment, Month Start, day Start) the same goes for the date the year ended.
Then I have a loop that goes through every single year and counts the amount of days you worked for that year between both dates.
Let us say for this EXAMPLE:
-The Date Started: January 2, 2020
-The Date Ended: June 23,2023
from datetime import timedelta, date
#For the Input Information
#Beginning Dates
print("===============================START WORK DATE INFO======================================\n")
YearStartEMP = int(input("Please Enter The Year Employment Started:"))
MonthStartEMP = int(input("Please Enter The Month Employment Started:"))
StartDayEmp = int(input("Please Enter The Day # Employment Started:"))
#End of Work dates
print("===============================END WORK DATE INFO======================================\n")
YearEndEMP = int(input("Please Enter The Year Employment Ended:"))
MonthEndEMP = int(input("Please Enter The Month Employment Ended:"))
EndDayEmp = int(input("Please Enter The Day# Employment Ended:"))
print("===============================DAYS PER YEAR WORKED=======================================\n")
#dates entered
StartWork = date(YearStartEMP, MonthStartEMP, StartDayEmp)
EndWork = date(YearEndEMP, MonthEndEMP, EndDayEmp) + timedelta(days=1)
#While loop to look through each year and count the days per year for each year betwwen dates
while StartWork < EndWork:
next_year = StartWork.replace(year=StartWork.year + 1, month=1, day=1)
if next_year < EndWork:
diff = next_year - StartWork
else:
diff = EndWork - StartWork
print(f'{StartWork.year}: {diff.days} days')
StartWork = next_year
This is what the Output would look like off the information I have given.
===============================START WORK DATE INFO=======================================
Please Enter The Year Employment Started:2020
Please Enter The Month Employment Started:1
Please Enter The Day # Employment Started:2
===============================END WORK DATE INFO=========================================
Please Enter The Year Employment Ended:2023
Please Enter The Month Employment Ended:6
Please Enter The Day# Employment Ended:23
===============================DAYS PER YEAR WORKED=======================================
2020: 365 days
2021: 365 days
2022: 365 days
2023: 174 days
The question I have is what code or function should I use for the part of code under "===DAYS PER YEAR WORKED===" to make the output look like this. what kind of if statements or functions should I use ASSUMING that a retiree only needs 180 days to get the credit for the year. It should look something like this.
2020: 365 days, 1 credit
2021: 365 days, 1 Credit
2022: 365 days, 1 Credit
2023: 174 days, 0 Credit
or something like this:
2020: 365 days
2021: 365 days
2022: 365 days
2023: 174 days
2020: 1 credit
2021: 1 Credit
2022: 1 Credit
2023: 0 Credit
I've tried doing this but it resulted in an error:
if DateBracket >= 180:
Credit = 1
print("1 credit")
elif DateBracket < 180:
Credit = 0
print("0 credit")
Here's your working code. I added some comments to explain the changes I made:
from datetime import timedelta, date
# For the Input Information
# Beginning Dates
print("===============================START WORK DATE INFO======================================\n")
# Added the `.strip()` method to remove whitespaces from the input
YearStartEMP = int(input("Please Enter The Year Employment Started: ").strip())
MonthStartEMP = int(input("Please Enter The Month Employment Started: ").strip())
StartDayEmp = int(input("Please Enter The Day # Employment Started: ").strip())
# End of Work dates
print("===============================END WORK DATE INFO======================================\n")
YearEndEMP = int(input("Please Enter The Year Employment Ended: ").strip())
MonthEndEMP = int(input("Please Enter The Month Employment Ended: ").strip())
EndDayEmp = int(input("Please Enter The Day # Employment Ended: ").strip())
print("===============================DAYS PER YEAR WORKED=======================================\n")
# Dates entered
StartWork = date(YearStartEMP, MonthStartEMP, StartDayEmp)
EndWork = date(YearEndEMP, MonthEndEMP, EndDayEmp) + timedelta(days=1)
# Also keep track of the total number of credits for simplicity
total_credits = 0
# While loop to look through each year and count the days per year for each year betwwen dates
while StartWork < EndWork:
next_year = StartWork.replace(year=StartWork.year + 1, month=1, day=1)
if next_year < EndWork:
# We only need the number of days worked in this year, not the whole timedelta
days_per_year = (next_year - StartWork).days
else:
days_per_year = (EndWork - StartWork).days
# Set `credits` to 1 if the employee worked at least 180 days in the year
# Else, set `credits` to 0
credits = 1 if days_per_year >= 180 else 0
# The code above is equivalent to the following, but it's cleaner
"""
if days_per_year >= 180:
credits = 1
else:
credits = 0
"""
# Also print the credits for the year
print(f'{StartWork.year}: {days_per_year} days, {credits} credit(s)')
StartWork = next_year
# Update the total number of credits
total_credits += credits
print(f'Total credits: {total_credits}')
And here's the sample output:
===============================START WORK DATE INFO======================================
Please Enter The Year Employment Started: 2020
Please Enter The Month Employment Started: 1
Please Enter The Day # Employment Started: 2
===============================END WORK DATE INFO======================================
Please Enter The Year Employment Ended: 2023
Please Enter The Month Employment Ended: 6
Please Enter The Day # Employment Ended: 23
===============================DAYS PER YEAR WORKED=======================================
2020: 365 days, 1 credit(s)
2021: 365 days, 1 credit(s)
2022: 365 days, 1 credit(s)
2023: 174 days, 0 credit(s)
Total credits: 3
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 months ago.
Improve this question
Im a complete noob in python and coding in general.... I was making a program to calculate the date and day of the next day of the week.... Here, a is the day of the month, b is the month c is the year d is the day of the week. And I wanted to know if we could leverage this to generate the date of a given day without having to enter the present day?
Try this:
import datetime
a = int(input('Enter a day: ')) # 10
b = int(input('Enter a month: ')) # 5
c = int(input('Enter a year: ')) # 2021
d = datetime.datetime(c, b, a).weekday() # 0
days = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
print(f'{a}/{b}/{c} - {days[d]}') # 10/5/2021 - Mon
Output:
Enter a day: 10
Enter a month: 5
Enter a year: 2021
10/5/2021 - Mon
I'm not completely sure what you're asking, but if you want to get the weekday (Monday/Tuesday/etc..) from any given date, you could use the variables to construct a date object. You can get the weekday from that as an integer 0-6 with the weekday() method or you could print the weekday name using the strftime() method.
Example:
import datetime
# day
a = 23
# month
b = 6
# year
c = 2022
input_day = datetime.date(day=a, month=b, year=c)
d = input_day.weekday()
print(d)
# Output: 3
d = input_day.strftime('%A')
print(d)
# Output: 'Thursday'
For more info: https://docs.python.org/3/library/datetime.html#date-objects
This question already has answers here:
How to convert datetime to integer in python
(7 answers)
Closed 1 year ago.
The task I was given is to create a program that will check if the current date is your birthday. If it is, print "Happy Birthday!". If not then output how many days left till your birthday.
I have been struggling with this task, I think I have got it working, but how do I remove the comma "," and the time "0:00:00" output from the result?
I only want it to display the number of days and the word days.
INPUT:
1989
6
21
DESIRED OUTPUT (at time/date of asking question!):
349 days
NOT: 349 days, 0:00:00
Hope that is clear and thanks in advance!
--
So far I have:
import datetime
today = datetime.date.today()
user_birth_year = int(input("Enter year of birth i.e. 1989: "))
user_birth_month = int(input("Enter month of birth i.e. for June enter 6: "))
user_birth_day = int(input("Enter day of birth i.e. for 21st enter 21: "))
my_birthday = datetime.date(today.year, user_birth_month, user_birth_day)
if my_birthday == today:
print("Happy Birthday!")
else:
if my_birthday < today:
my_birthday = my_birthday.replace(year=today.year + 1)
days_until_birthday = my_birthday - today
print(days_until_birthday)
else:
days_until_birthday = my_birthday - today
print(days_until_birthday)
This is included in any tutorial on datetime. If you want only the days, then access the days attribute.
else:
days_until_birthday = my_birthday - today
print(days_until_birthday.days, "days")
So I am writing a program to work out the number of days you have been alive after imputting your birthday. There is a problem as i am getting the wrong number of days but can figure out why. i inputted my birthday as 04/04/19 and i got 730625 days which is clearly wrong.
import datetime #imports module
year = int(input("What year were you born in"))
month = int(input("What month where you born in (number)"))
date = int(input("What date is your birthday? "))
birthdate = datetime.date(date, month, year) #converts to dd/mm/yy
today = datetime.date.today() #todays date
daysAlive = (today - birthdate).days #calculates how many days since birth
print("You have been alive for {} days.".format(daysAlive)) #outputs result
I initially got the same error as you but then I checked my code and managed to fix my mistake.
So your DOB is 04/04/19, when you input that into datetime.date() and it looks at the value for year which is 19, it will treat that as 0019. As in 19 AD, not 2019. You should make sure that you input the full year.
Also like SimonN said, the parameters for datetime.date() are year, month, day, not the other way around.
You have the parameters the wrong way round in datetime.date they should be (year,month,day)
datetime takes arguments as (year, month, date). Note that you cannot enter year like 09 for 2009. Datetime will count it as 0009-MM-DD. You have to enter complete year in the input as 2009
...
birthdate = datetime.date(year, month, date)
...
So, with your input, the output for me is (It may differ with your timezone):
You have been alive for 170 days.
class datetime.date(year, month, day)
should be in the format yy/mm/dd.
Try this code for Python 3.6 or higher,
because of f-stings:
import datetime
year = int(input("What year were you born in: "))
month = int(input("What month were you born in (number): "))
day = int(input("What day were you born in: "))
birth_date = datetime.date(year, month, day) # converts to yy/mm/dd
today = datetime.date.today() # todays date
days_alive = (today - birth_date).days # calculates how many days since birth
print(f"You are {days_alive} days old.") # outputs result
Check the answer using other sources.
I wrote a function that contains a dictionary of abbreviated week days to the full name of the day. I get the proper output day when I type in the abbreviation, but in order to try another abbreviation, I have to retype the function.
I have:
def weekday()
day = input('Enter day abbreviation ' )
days = {'Mo':'Monday','Tu':'Tuesday',
'we':'Wednesday', 'Th':'Thursday',
'Fr':'Friday', 'Sa':'Saturday','Su':Sunday'}
while day in days:
print(days.get(day))
The problem I have is that it prints the full day name over and over again, and instead I want it to print the full day name, then print 'Enter day abbreviation' again.
It should look like this:
>>>weekday():
Enter day abbreviation: Tu
Tuesday
Enter day abbreviation: Su
Sunday
Enter day abbreviation:
...
Instead, I get:
>>>weekday():
Enter day abbreviation: Tu
Tuesday
Tuesday
Tuesday
Tuesday
Tuesday
... # it continues without stopping
I know this is a really simple solution, but I can't figure it out.
You never reread "day" so "while day in days" is always true and executing endlessly.
def weekday()
day = input('Enter day abbreviation ' )
days = {'Mo':'Monday','Tu':'Tuesday',
'we':'Wednesday', 'Th':'Thursday',
'Fr':'Friday', 'Sa':'Saturday','Su':'Sunday'}
while day in days:
print(days.get(day))
day = input('Enter day abbreviation ' )
You want to get input again in every iteration:
while True:
day = input('Enter day abbreviation ' )
acquired_day = days.get(day)
if acquired_day is None: break
print(acquired_day)
days = {'Mo':'Monday','Tu':'Tuesday',
'we':'Wednesday', 'Th':'Thursday',
'Fr':'Friday', 'Sa':'Saturday','Su':'Sunday'}
while True:
day = input('Enter day abbreviation ' )
if day in days:
print (days[day])
else:
break
output:
$ python3 so.py
Enter day abbreviation Mo
Monday
Enter day abbreviation Tu
Tuesday
Enter day abbreviation we
Wednesday
Enter day abbreviation foo
Another way using dict.get:
days = {'Mo':'Monday','Tu':'Tuesday',
'we':'Wednesday', 'Th':'Thursday',
'Fr':'Friday', 'Sa':'Saturday','Su':'Sunday'}
obj = object() #returns a unique object
day = input('Enter day abbreviation ' )
while days.get(day,obj) != obj:
print (days[day])
day = input('Enter day abbreviation ' )