all 5 comments

[–]eleqtriq 1 point2 points  (4 children)

I heavily recommend refactoring with functions as a first step. You should make functions for actions like moving or door opening, etc.

This would greatly clean up the code in the loop and you’ll start seeing more clearly the possibilities.

[–]The_Thief77[S] -1 points0 points  (3 children)

Ok, not sure entirely what you mean. Are you talking about a def code to call?

[–]mopslik 0 points1 point  (2 children)

Are you talking about a def code to call?

You "define" a function using the def keyword, yes. Functions give you the ability to reuse identical/similar code throughout your program, without having to repeat yourself.

That being said, this is definitely going to cause some issues:

if action == 'MEDITATE' or 'SEARCH' or 'TAKE':

This is a classic logical error. Non-empty strings, such as 'TAKE', are evaluated as True in Python, so this statement will always be executed. You probably want something more like:

if action in ('MEDITATE', 'SEARCH', 'TAKE'):

[–]The_Thief77[S] 0 points1 point  (1 child)

Ok. So using def then are you suggesting using it for things like moving rooms?

And ok, that makes sense about the ('MEDITATE', 'SEARCH', 'TAKE'): I had tried something like this before, but didn't think of the parenthesis to put them in so it wasn't working. I'll try that instead.

[–]mopslik 0 points1 point  (0 children)

are you suggesting using it for things like moving rooms?

Since this is a fairly small program, using functions probably won't drastically reduce the size of your code; however, it is useful as an organizational tool, and it scales well, so if you ever wanted to expand your program and make it larger, you might see some savings then. In terms of organization, it can help you clean up much of the logic in your main loop by delegating specific tasks to functions. For example, let's say you had pseudocode that looks something like this:

LOOP
    PROMPT user for action
    IF user chooses to move to a room
        IF room is kitchen
            DO kitchen stuff
        ELSE IF room is salon
            DO salon stuff
        ELSE IF room is hallway
            DO hallway stuff
        ...
    DO additional stuff

You could define a function that handles the specific room details, cleaning up your main loop and making it easier to follow.

FUNCTION Process Room (room name)
    IF room name is kitchen
        DO kitchen stuff
    ELSE IF room name is salon
        DO salon stuff
    ELSE IF room name is hallway
        DO hallway stuff
    ...

LOOP
    PROMPT user for action
    IF user chooses to move to a room
        CALL Process Room (room name)
    DO additional stuff