0

I created a function to get date from the user and convert it into datetime format using strptime. The function is called Obtaindate(). This will go as input into another function Getdata(). Now, in the second function Getdata() I need to subtract the date from Obtaindate() with timedelta. But I get the error TypeError: unsupported operand type(s) for -: 'NoneType' and 'datetime.timedelta'.

What should I do to get the date from the 1st function to work in the second function?

Here is the code sample:

def ObtainDate():
   
    isValid=False

    while not isValid:

        userin = str(input("Type Date ddmmyy: "))
        
        try: 
            dt = datetime.datetime.strptime(userin, '%d%m%y')
            print('You have selected {}'.format(datetime.date.strftime(dt, '%d-%b-%Y')))
            break
        except:
            print("Incorrect format...try again!")
            continue

            return dt

ObtainDate()
    
def Getdata():
    
    date1 = ObtainDate() - datetime.timedelta(1)
    date2 = datetime.datetime.today()
    delta = datetime.timedelta(1)
    
    while date1 < date2:

        print('Download in progress...')
wjandrea
  • 28,235
  • 9
  • 60
  • 81
Rockoos
  • 37
  • 8
  • Did you return a value from the first function? It looks like it is returning `None`. – progmatico May 08 '21 at 13:25
  • In the `Obtaindate` function, use `return dt` instead of `break`. As your code is written in the example, the return will never be reached since it is placed *after* `continue`. Therefore, the function returns `None` as you only break the while loop without returning something. – FObersteiner May 08 '21 at 15:34
  • 1
    Beside the point, but [a bare `except` is bad practice](/q/54948548/4518341), especially on a multi-line `try`-block. Instead, use the specific exception you're expecting like `except ValueError`, or at least `except Exception`, and use an `else`-block for code to run if no exception occurs. – wjandrea May 08 '21 at 15:50
  • `return dt` is indented two times more than it should be. Voting to close as typo. – wjandrea May 08 '21 at 15:51
  • 1
    @wjandrea jup, an `else: return dt` would be best I think, in terms of readability. – FObersteiner May 08 '21 at 16:14
  • Thank you all for your valuable comments, I wanted to highlight some particular ones and reply directly but unable to. – Rockoos May 08 '21 at 17:19

1 Answers1

0

Try this - comments inlne to show add.mmove/delete

The except isn’t very nice - better to catch the specific exception(s)


def ObtainDate():
   
    isValid=False

    while not isValid:

        userin = str(input("Type Date ddmmyy: "))
        
        try: 
            dt = datetime.datetime.strptime(userin, '%d%m%y')
            print('You have selected {}'.format(datetime.date.strftime(dt, '%d-%b-%Y')))

            return dt    ####### moved here
        except:
            print("Incorrect format...try again!")
            continue

# REMOVED        return dt

def Getdata(adate): # ADDED PARAMETER
    
    date1 = adate- datetime.timedelta(1) # USE PARAMETER
    date2 = datetime.datetime.today()
    delta = datetime.timedelta(1)
    
    while date1 < date2:

        print('Download in progress...')
        # ADDED SAVING DATA
        data_for_this_date = …

    # ADDED RETURN
    return data_for_this_date

# ADDED SAVING VALUE OF FUNCTION CALL
theday = ObtainDate()
# ADDED PASSING the result from Obtaindate()
# AND ADDED store result returned by Getdata in a variable
data = Getdata( theday )