1

I have a menu, where I input a start and end-date. I get an error when I select choice 2 in below menu-item.

    ### Take action as per selected menu-option ###
    if choice == 1:  # Start with default window
        print ("Starting Backtest from 2014-1-1 until 2015-12-31")
        start = datetime.datetime(2014,1,1)
        start = start.date()
        end   = datetime.datetime(2015,12,31)
        end   = end.date()
    elif choice == 2:
        ## Get input ###
        start = input('Enter Start date [Format: YYYY,M,D  -  2014,1,23] : ')
        start = datetime.datetime(start)
        start = start.date()
        end   = input('Enter End date   [Format: 2015,8,23] : ')
        end   = datetime.datetime(end)
        end   = end.date()

How is it possible that my menu-item 1 works but 2 doesn't?

The error is

TypeError: an integer is required (got type str)

Please help

Thanks

Excaliburst
  • 143
  • 1
  • 4
  • 15
  • 1
    Looks like you're getting that error because you're using a string somewhere that an integer is supposed to be used. Hint: what line is the error occurring on? – Kevin Feb 24 '16 at 18:09
  • I get the error in this line start = datetime.datetime(start) – Excaliburst Feb 24 '16 at 19:50

2 Answers2

0

You might be getting that error because you are comparing a string to an integer. You can change between the two like so

>>> int('123')
>>> 123
>>> str(123)
>>> '123'
Igor
  • 1,212
  • 12
  • 23
0

OK - after some scouring about, I found this question that helped me to the right answer. TypeError: descriptor 'strftime' requires a 'datetime.date' object but received a 'Text'

So I needed this code to get my script running smoothly. Under choice 2 you see the new code with strptime and strftime. Apparantly I need to go through strptime to get to strftime!

    ### Take action as per selected menu-option ###
    if choice == 1:  # Start with default window
        print ("Starting Backtest from 2014-1-1 until 2015-12-31 (2 years)")
        start = datetime.datetime(2014,1,1)
        start = start.date()
        end   = datetime.datetime(2015,12,31)
        end   = end.date()
    elif choice == 2:
        ## Get input ###
        start_input = input('Enter Start date [Format: YYYY,M,D  -  2014,01,23] : ')
        start_dt_obj = datetime.datetime.strptime(start_input, '%Y,%m,%d')
        start = datetime.datetime.strftime(start_dt_obj, '%Y,%m,%d')

        end_input   = input('Enter End date   [Format: YYYY,M,D  -  2015,08,23] : ')
        end_dt_obj = datetime.datetime.strptime(end_input, '%Y,%m,%d')
        end = datetime.datetime.strftime(end_dt_obj, '%Y,%m,%d')

However this makes my code run, but I would have like to keep the date as a date. The choice 2 code saves my date as a str unlike Choice 1. Hmm...

Community
  • 1
  • 1
Excaliburst
  • 143
  • 1
  • 4
  • 15