as part of a game I'm making I decided to make an importable/executable file to shorten the code and I was wondering...

def view(N,W,E,S):
    if info_dict["direction"] == "north":
        if N != 1:
            pic = fwall
            wall = "yes"
        if N == 1:
            if W != 1 and E != 1:
                pic = fhall
                wall = "no"
            if W == 1 and E != 1:
                pic = flhall
                wall = "no"
            if W != 1 and E == 1:
                pic = frhall
                wall = "no"
            if W == 1 and E == 1:
                pic = frlhall
                wall = "no"
    if info_dict["direction"] == "west":
        if W != 1:
            pic = fwall
            wall = "yes"
        if W == 1:
            if S != 1 and N != 1:
                pic = fhall
                wall = "no"
            if S == 1 and N != 1:
                pic = flhall
                wall = "no"
            if S != 1 and N == 1:
                pic = frhall
                wall = "no"
            if S == 1 and N == 1:
                pic = frlhall
                wall = "no"
    if info_dict["direction"] == "south":
        if S != 1:
            pic = fwall
            wall = "yes"
        if S == 1:
            if E != 1 and W != 1:
                pic = fhall
                wall = "no"
            if E == 1 and W != 1:
                pic = flhall
                wall = "no"
            if E != 1 and W == 1:
                pic = frhall
                wall = "no"
            if E == 1 and W == 1:
                pic = frlhall
                wall = "no"
    if info_dict["direction"] == "east":
        if E != 1:
            pic = fwall
            wall = "yes"
        if E == 1:
            if N != 1 and S != 1:
                pic = fhall
                wall = "no"
            if N == 1 and S != 1:
                pic = flhall
                wall = "no"
            if N != 1 and S == 1:
                pic = frhall
                wall = "no"
            if N == 1 and S == 1:
                pic = frlhall
                wall = "no"

would this be usable and if so how would I do so?

Recommended Answers

All 4 Replies

First I think it can be shortened to

def view(N,W,E,S):
    t = (N, W, S, E)
    st = ("north", "west", "south", "east")
    ft = (fhall, lfhall, frhall, frlhall)

    d = st.index(info_dict["direction"])
    if t[d] != 1:
        pic = fwall
        wall = "yes"
    else:
        x, y = t[(d+1) % 4], t[(d+3) % 4]
        x, y = int(not bool(x-1)), int(not bool(y-1)) # now != 1 means 0
        pic = ft[2 * y + x]
        wall = "no"

Otherwise, to make an importable module, simply save the file as "mymodule.py" and write "import mymodule" in the other files.

Don't program with copy and paste !

Also, use the 'else' keyword in sequences of if statements.

ok I can import but how would I use it would I just type mymodule.view(1,0,0,0)?

ok imported and used ... how would I get the information back to be used in my current program?

ok imported and used ... how would I get the information back to be used in my current program?

If you want information back, there are different ways, you can let your function return a value, then

from mymodule import view

information = view(...)

Or you could make view a method in a class and set instances attributes

class Whatever(object):
  def view(self, ...):
    ...
    self.pic = ...

Then in other files

from mymodule import Whatever

w = Whatever()
w.view(0,0,...)
print w.pic

There are many other ways to get information back...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.