0

I have made a function in the format: day/month/year. I would like to make another function that asks the user for example: "When do you want to come?" and depending on what month the user say I want to print out a answer.

The problem: I don't know how to make the function that asks the user for the format: day/month/year without limit the years, I would like the user to enter any year and still be able to get the same answers as another any year (but different month).

enter code here
import datetime 

def dateformat(date):
    return datetime.datetime.strptime(datumet, "%d/%m/%Y")

def ask_user():
    winter = dateformat('1/1/2020') <= dateformat('31/3/2020')
    spring = dateformat('1/4/2020') <= dateformat('31/5/2021')
    summer = dateformat('1/6/2020') <= dateformat('31/9/2021')
    autumn = dateformat('1/10/2020') <= dateformat('31/12/2021')
    
    a = dateformat(input("When do you want to come"))
    if a == winter:
        print("Hi")
    if a == spring:
        print("bye")
    if a == summer:
        print("ok")
    if a == autumn:
        print("no")

My question: How can I make this code work for any year? I would like to be able to type any year but inside the month and get the same output. If I only return %d/%m in the dateformat-function the user will not be able to type: day/month/year. Is there maybe a better way of returning the format?

5
  • From the input you can just extract the day and month. This will allow the user to type in any year. Then, this If I only return %d/%m in the dateformat-function the user will not be able to type: day/month/year will not be a problem. The season does not depend on the year. Commented Nov 30, 2020 at 17:04
  • @MacOS how do you mean? How can I make winter all the moths that I had without caring about which year? Commented Nov 30, 2020 at 17:06
  • 1
    I don't think your code does what you expect it to do. For one, 31/9/2021 is not an actual date so dateformat would throw an error. Second, winter, spring, etc. are booleans. You're essentially comparing a date to a boolean, so they'll never be equal and you won't get any output. Commented Nov 30, 2020 at 17:07
  • @PranavHosangadi Do you have an example? I see what you mean. Commented Nov 30, 2020 at 17:09
  • @PranavHosangadi How can I make winter for example: 01/12<= 31/3, but make the user type the year too? Commented Nov 30, 2020 at 17:10

1 Answer 1

2

I don't think your code does what you expect it to do. For one, 31/9/2021 is not an actual date so dateformat would throw an error. Second, dateformat('1/1/2020') <= dateformat('31/3/2020') checks if the first date is less than or equal to the second, so winter, spring, etc. are booleans (and all True). With a == winter, you're comparing a date to a boolean, so they'll never be equal and you won't get any output.

What you actually want to do is read in the date, and see if its month attribute is between certain limits, because the value of a.year doesn't affect the season. So:

datumet = input("When do you want to come? ")
a = datetime.datetime.strptime(datumet, "%d/%m/%Y")
if a.month <= 3:
    print("Hi, you're coming in winter")
elif a.month <= 5:
    print("Spring")
elif a.month <= 9:
    print("Summer")
else:
    print("Autumn")

The same applies when your ranges don't end at the end of the year. For example, if winter lasted the start of November through the end of February,

if a.month >= 11 or a.month <= 2:
    print("Hi, you're coming in winter")
elif a.month <= 5:
    print("Spring")
elif a.month <= 9:
    print("Summer")
else:
    print("Autumn")
Sign up to request clarification or add additional context in comments.

13 Comments

@Carl-ErikPettersson i.sstatic.net/wwPXM.png Can you elaborate? It works perfectly fine for me.
Sorry! I typed wrong! But I noticed one thing, the code wont work with for-loops, I created a list with numbers and would like to make it all printed out when a specific month gets called but it didnt work. It only gave me the first element in the list
I can show you how I mean by editing my orginal question check it please :)
Do that. I'm not sure what you mean, so seeing your code will help.
never mind I wrote wrong again! Thanks again sir!! :)))
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.