1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.
  2. Anuncie Aqui
    Anuncie aqui você Também: fdantas@4each.com.br

[Python] Why am I getting a return value of None when I used return, but it works in all other...

Discussão em 'Python' iniciado por Stack, Setembro 14, 2024.

  1. Stack

    Stack Membro Participativo

    # Sample Code
    import datetime

    sample = {
    "Eat": [
    datetime.datetime(2024, 9, 13, 2, 0),
    datetime.datetime(2024, 9, 13, 3, 0),
    "2024-09-13 20:43:35.440898",
    ],
    "Sleep": [
    datetime.datetime(2024, 9, 13, 4, 0),
    datetime.datetime(2024, 9, 13, 20, 0),
    "2024-09-13 20:43:24.354440",
    ],
    }


    # This is the entire function that I am using
    def SortObjectives(obj):
    temLis = []
    temDic = {}

    for i in obj:
    print(str(obj[0].time()))
    temLis.append(obj[0].time())

    temLis.sort()
    print(temLis)
    temCou = 0

    # Recursive Function
    def SortObjectivesR(count):
    for i in obj:
    print(count)
    print(str(temLis[count]) + " --From temLis)")
    if obj[0].time() == temLis[count]:
    temDic = obj

    print(len(temDic))
    print(len(obj))

    if len(temDic) == len(obj):
    print("Here")

    print(temDic)

    return temDic
    # Why does this return None when it has a literal value???
    # Keep in mind that this function only does this when their are two items
    # in the obj dictionary when their not in order
    # It works when in order and when in the number of items is > or < then 2

    else:
    count += 1
    SortObjectivesR(count)
    # return temDic - not part of original code

    print(SortObjectivesR(temCou))
    # How I find what it returns
    # Would put in return statement to save lines, but it returns None
    # Under the specific condition mentioned, the alteration of
    # Variables work just fine.

    return temDic


    print(SortObjectives(sample))


    I have tried changing the order of the items and number of items and testing it to see if it would recreate the issue. My hope with this was to see if there was a similarity that I could point out, but it is specific too what I have pointed out in the code. I don't get it at all, it makes no sense to me. I also tried returning simple values like just a string into the return and it ended up as None as well.

    Comments You May Skip Over:

    Why does this return None when it has a literal value???

    Keep in mind that this function only does this when their are two items in the obj dictionary when their not in order It works when in order and when in the number of items is greater or lesser then 2 WOuld put in return statement to save lines, but it returns None Under the specific condition mentioned, the alteration of Variables work just fine.

    Continue reading...

Compartilhe esta Página