In your case, when previous value is empty, previous in 'dD'
returns true
.
So in your code, from the first part, it return flag_di=True
As a result the output is 1
.
How to fix?
You can add add some code part that check previous is empty or not.
if previous and car in 'iI' and previous in 'dD': # Check if previous is not empty
flag_di = True
Then answer will be 0