0

I have a main program that uses a function from a class from another file, in the while loop, when the function is called with the correct parameters it just doesn't return anything print anything or do anything it supposed to do, however it doesn't raise any errors and it goes back to the start of the loop.

Here is a snippet of the main loop in question:

....
elif command in ["talk","fight","hug"]:
    if inhabitant is not None:
        if inhabitant.event_handler(command,current_room,inventory) is False:
            break
        else:
            continue
    else:
        print("There is nobody in the room")
        cl()
....

and the function in question:

def event_handler(self,command,room,inv):
    if command is "talk":
        self.talk()
        input()
        print("\n"*25)
    elif command is "hug":
        print("You probably don't want to do that")
        input()
        print("\n"*25)
    elif command is "fight":
        itemSel = input("What item do you want to fight with?\n>")
        if itemSel in inv:
            if self.fight(itemSel):
                room.character = None
                if Character.number_of_enemies == 0:
                    print("Congrats! You defeated all the enemies and won the game!")
                    input()
                    return False
                input()
                print("\n"*25)
            else:
                print("You died")
                input()
                return False
        else:
            print("You don't have this item")
            input()
            print("\n"*25)

where all of the variables are valid and have already been assigned values at some point in the main loop, and the functions .fight() and .talk() are also in the same class as .event_handler()

.event_handler() is a method of the object stored as inhabitant

What could be the reason behind this not working? I don't really get it so if anyone could help I'd greatly appreciate it.

Mr.Cag
  • 9
  • 1
  • How are you determining that the function is being called? – jordanm Aug 10 '20 at 16:53
  • try to put some loggings or use a debugger to see your current code flow – Phu Ngo Aug 10 '20 at 16:55
  • @jordanm what exactly do you mean by "How are you determining that the function is being called?" on the line "if inhabitant.event_handler(command,current_room,inventory) is False:" you can see "inhabitant.event_handler(command,current_room,inventory)" which is the function – Mr.Cag Aug 10 '20 at 16:58
  • 1
    Those `is` comparisons for strings are likely to fail. Use `==` instead. See https://stackoverflow.com/q/132988/5987 – Mark Ransom Aug 10 '20 at 16:59
  • 1
    See this [lovely debugging site](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/) for help. Please supply the expected [minimal, reproducible example](https://stackoverflow.com/help/minimal-reproducible-example). Show where the intermediate results differ from what you expected. – Prune Aug 10 '20 at 16:59
  • @Mr.Cag How do you know it's not being called there due to "inhabitant" always being none or some other issue? – jordanm Aug 10 '20 at 17:04
  • @jordanm inhabitant is parsed to other functions earlier in the loop, and they use inhabitant in a similar way to event_handler, but they don't change the variable, thus `if inhabitant is not None:` is True. The problem was that I used `is` to compare strings instead of `==` as @Mark Ransom mentioned above – Mr.Cag Aug 10 '20 at 17:48

1 Answers1

0

I think you have multiple problems going on here, I am afraid. First, event_handler has no return statement. Therefore, no matter what, it is returning None, which is not False. Therefore,

if inhabitant.event_handler(command,current_room,inventory) is False:

will always be untrue.


However, as @jordanm mentioned, you are likely not even getting to that call. You can test this with a small function just to see what would have happened:


>>> def testfun():
...   print("Did I get here?")
... 
>>> if testfun() == False:
...   print("Yes, I got in here.")
... 
Did I get here?

You'll notice 2 things:

  1. Even though there is no return statement (which means it always returns None), the function is at least called.
  2. The comparison (if statement) is untrue, since False != None, so the second print statement is not run.

Finally, as others have said, using is is risky. Without inundating you with too much information, == compares if the values are the same whereas is compares if it is actually the same object in memory. If that makes no sense to you, don't worry about it and just remember that == is the safer route.

Mike Williamson
  • 4,915
  • 14
  • 67
  • 104
  • event_handler returns False only in two circumstances, one is if `if self.fight(itemSel):` returns False, and the other is when `if Character.number_of_enemies == 0:` returns True (Character.number_of_enemies is decremented within fight() every time it returns True) apart from these two circumstances the function just does other stuff. For any other circumstance the loop continues `else: continue` The problem was that I used `is` instead of `==`, changing this fixed it – Mr.Cag Aug 10 '20 at 17:43
  • @Mr.Cag Yes, you're right. I did not look carefully enough. But it is usually good practice to return the same type in all your paths. So if some paths return `False`, then all should return a `bool` type. This is not *required* in Python, as it is in strongly typed languages like Haskell or Rust, but it is harder to think through your code if you do not do this (including being unable to use Python's new [`typing`](https://docs.python.org/3/library/typing.html) module). – Mike Williamson Aug 10 '20 at 18:57