1
votes

i tried to make function, where i can choose the date. Like today, yesterday or random day from 2000-01-01

Here is my try, but cant and dont understand a little bit how to make it right. Thank you!

from datetime import date, timedelta
import os 

os.system('cls')


def date():
    s = input("Write 1 for today, 2 for yesterday and 3 for random date: ")
     
    if s == 1:
        
        link_today = "https://api.nasa.gov/planetary/apod?api_key=DEMO_KEY&date=" + str(date.today())
        return link_today
    elif s == 2:
        yesterday = date.today() + timedelta(days=-1)
        print(yesterday)
        return yesterday
        
date()
You're importing date from datetime. Don't call your function datenot_speshal
Also input() returns str and you compare s with int value (1 or 2) - they will never be equal.buran
You get string when you use input func. You have to convert it to intbichanna
RIght. get_date would be better. And, by the way, os.system('cls') is not a good practice. You don't know whether the user had something on their screen they wanted to keep for later. Plus, it's Windows-only. And remember that input returns a string, not an integer. You need if s == '1':Tim Roberts
The input you receive from input will be a string, but you are comparing it to integers. I would recommend casting s to int or changing your if statements to compare using strings like so: if s == '1':VoidTwo