Click here to Skip to main content
14,299,779 members
Rate this:
Please Sign up or sign in to vote.
See more:
I cannot get the second if statement in my code to return what I want which is [current date '11:00:00'].
It only returns [current date '09:00:00']

What I have tried:

def today_hours():
    today_date = []
    today_date.append(return_week_day()[0:10])
    for key, value in hourly_dates.items():
        if key in first_indices:
            if value == True:
                today_date.append(return_week_day()[11:19])
            else:
                if value == morning_hours:
                    today_date.append('09:00:00')
                else:
                    if value == noon_hours:
                        today_date.append('11:00:00')
            return today_date

print(today_hours())
Posted
Updated 17-Jul-19 11:26am
v2
Comments
Richard MacCutchan 18-Jul-19 3:35am
   
You are trying to use the variable value to represent different types:True, morning_hours and noon_hours, so the if statements will be confused.
Member 13949923 18-Jul-19 6:48am
   
I think u might be on to something here but I don't fully get it. Would be kind and reformulate what u mean?
Richard MacCutchan 18-Jul-19 8:19am
   

for key, value in hourly_dates.items():


What is returned in the variable named value?
Member 13949923 19-Jul-19 7:34am
   
hourly_dates = {'morning': morning_hours, 'noon': noon_hours, 'afternoon': afternoon_hours, 'evening': evening_hours, 'night': night_hours}

If the there is an input such a 'Today evening' and the datetime is in the 'evening' parameter(between 18:00:00 - 21:00:00) it returns 'True' and then prints [current date '19:00:00'] but if input is 'Today morning' it should get 'False' for the first if statement 'Value == True' and then move to second if statement (Value == 'morning_hours') after else statement and so on. But when I input 'Today noon' it does not move to next if statement that has the 'Value == noon_hours'. It just keep printing if statement of 'morning_hours' and don't move the next statement.
Richard MacCutchan 19-Jul-19 8:12am
   
You cannot compare value to True or False, since it will contain one of the values in the hourly_dates list.
Richard MacCutchan 19-Jul-19 10:46am
   
Since we cannot see the data you are working with it is impossible to answer. Use the debugger to step through the code and see exactly what you are comparing at each if statement. For example what is in the variables key and first_indices, at the beginning.
Member 13949923 19-Jul-19 10:45am
   
thank you so much for all the help, solved it now.

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

For comments on your code, you need to include the variables it use.

Quote:
I cannot get the second if statement in my code to return what I want which is [current date '11:00:00'].
It only returns [current date '09:00:00']

Your code do not behave the way you expect, or you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your code is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

27.3. pdb — The Python Debugger — Python 3.6.1 documentation[^]
Debugging in Python | Python Conquers The Universe[^]
pdb – Interactive Debugger - Python Module of the Week[^]

The debugger is here to only show you what your code is doing and your task is to compare with what it should do.
   
v2
Comments
Member 13949923 18-Jul-19 6:46am
   
I have tried debugging myself and with Visual Studio with no veil. Because my code returns an answers [current date '09:00:00'] although I ask for [current date '11:00:00'], the debug shows no problem. I think my problem is with the logic syntax of Python. It should go to next if statement after "value == morning_hours" shows False. But it doesn't.
Patrice T 18-Jul-19 6:53am
   
You need to learn the debugger. correct usage allow you to execute your code step by step and inspect variables.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100