#๐Ÿ”’ Why am I getting <class 'NoneType'> For the following

27 messages ยท Page 1 of 1 (latest)

spring copper
#
  def check_difference(machine, customer):
        for key in machine:
            if Counter(machine[key]) > Counter(customer[key]):
                print("here is your drink")
            else:
                print(f"There is not enough {key}")
                return machine, customer


    difference = check_difference(machine_resources, users_drink)
    print(difference)
    print(type(difference))

```py
zealous axleBOT
#

@spring copper

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

lethal badger
#

you don't return anything in some cases

spring copper
#

Meaning?

#

But if there isnt enough resources in the machine then it returns a dictionary

lethal badger
#

This is what your code looks like:

def check_difference(machine, customer):
        for key in machine:
            if Counter(machine[key]) > Counter(customer[key]):
                print("here is your drink")
            else:
                print(f"There is not enough {key}")
                return machine, customer

        return None  # This is implicit!
#

so you return only in the else branch

spring copper
#

ohhhh I see

minor flare
#

all functions have an invisible default return statement at the end of them, which returns None

#

So you'll need to override that one, and cover all executions paths, in order to have consistent returns

spring copper
#

So is this correct:


  def check_difference(machine, customer):
        for key in machine:
            if Counter(machine[key]) > Counter(customer[key]):
                print("here is your drink")
                return machine, customer
            else:
                print(f"There is not enough {key}")
                return machine, customer
```py
minor flare
#

From a syntactiv POV, it's correct

#

Functionally, it's doing the same thing in the two branches of the "if" condition

#

And the loop becomes useless

spring copper
minor flare
#

!e

def dosomething():
  print("hello")

var = dosomething()
print(var)
zealous axleBOT
#

@minor flare :white_check_mark: Your 3.12 eval job has completed with return code 0.

001 | hello
002 | None
minor flare
spring copper
minor flare
spring copper
#

So its irelivant becase I am already using an if statement in a function

#

Am I correct?

minor flare
#

It's mostly because you're saying "if not X then ALWAYS Y"

#

Which doesn't leave room to your loop to move on

spring copper
#

That makes sense

zealous axleBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.