Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
[Python] Condition-statement not doing what I want
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Portage & Programming
View previous topic :: View next topic  
Author Message
kgdrenefort
Apprentice
Apprentice


Joined: 19 Sep 2023
Posts: 186
Location: Somewhere in the 77

PostPosted: Thu Apr 04, 2024 2:38 pm    Post subject: [Python] Condition-statement not doing what I want Reply with quote

Hello,
(not doing any true serious Python learning, barely having fun, my skills in programming are very low and I do not follow any books or regular courses, which is intended from my side)

so far as I understand, minors programming issue could be asked here, so here we go with a Python3.12 issue I have since two days:

I have 3 files I work with:

- main.py
- GoTDA_variables.py
- GoTDA_functions.py
(side note: I pasted the actual state of each file, without any cleaning, at least you have the whole code here, sorry it’s kind of a mess, samples below)

’main’ is obviously running while importing other piece of code all the calls to my functions inside ’GoTDA_functions’, and from some variables into the last file ’GoTDA_variables’.

My goal is to draw with ASCII characters a frame, printed with corner top/left and bottom/right, each side has it’s own key into a dictionary:

Code:
# This dictionnary hold all printables characters to draw the frame and it's area (cells)
charsDic = {
    "CTL" : "┏", # Corner, top, left
    "CBL" : "┗", # Corner, bottom, left
    "CTR" : "┓", # Corner, top, right
    "CBR" : "┛", # Corner, bottom, right
    "BOT" : "━", # Border, top
    "BOB" : "━", # Border, bottom
    "BOL" : "┃", # Border, left
    "BOR" : "┃", # Border, right
    "ZON" : "·", # Zone, within the frame
            }


I have a function that is used to order characters into a simple list. For each dimension (x & y) it looks what are each dimensions value and decide what is it’s key from the dir above. This is working:

Code:
['┏', '━', '┓', '┃', '·', '┃', '┗', '━', '┛']


And in the correct order, once the next function will display it properly.

This was generated from this extract of my function:

Code:
    for y in range(0, height, 1): # For each column (ordonate)
        for x in range(0, lenght, 1): # For each row (abscissa)

            # 1: Origin, CTL (corner, top-left)           
            if x == ORIGIN \
            and y == ORIGIN:
                listing.append(charsDic["CTL"])

            # 2: First row, any non-first & non-last column, BOT (border-top)
            elif y == ORIGIN \
            and x != ORIGIN \
            and x != lenght-1:
                listing.append(charsDic["BOT"])

            # 3: First row, last column, CTR (corner-top-right)
            elif y == ORIGIN \
            and x == lenght-1:
                listing.append(charsDic["CTR"])

            # 4: Any non-firt & non-last row, BOL (border-left)
            elif y != ORIGIN \
            and y != height-1 \
            and x == ORIGIN:
                listing.append(charsDic["BOL"])

            # 5: Any non-first & non-last row, any non-first & and non-last column, ZON (zone)
            elif y != ORIGIN \
            and y != height-1 \
            and x != ORIGIN \
            and x != lenght-1:
                listing.append(charsDic["ZON"])

            # 6: Any non-first & non-last row, last column, BOR (border-right)
            elif y != ORIGIN \
            and y != height-1 \
            and x == lenght-1:
                listing.append(charsDic["BOR"])

            # 7: Last row, first column, CBL (corner-bottom-left)
            elif y == height-1 \
            and x == ORIGIN:
                listing.append(charsDic["CBL"])

            # 8: Last row, any non-first & non-last column, BOB (border-bottom)
            elif y == height-1 \
            and x != ORIGIN \
            and x != lenght-1:
                listing.append(charsDic["BOB"])

            # 9: End, last row, last column, CBR (corner-bottom-right)
            elif y == height-1 \
            and x == lenght-1:
                listing.append(charsDic["CBR"])

    return listing


Then, I want to pass that list to another function, supposed to merely print these characters in the same order but ordered to become a frame/square, containing a zone which in this case is filled with the characters ’·’, could be anything:
Code:

    pos = 0
    for y in range(0, height, 1):
        for x in range(0, lenght, 1):

            # CTL
            if listing[x] == charsDic["CTL"] \
            and ((x == 0) and (y == 0) and (pos == 0)):
                print(listing[pos], charsDic["BOT"], sep="", end="")

            # BOT
            elif listing[x] == charsDic["BOT"] \
            or ((x == 1) and (y == 0) and (pos == 1)):
                print(listing[pos], charsDic["BOT"], sep="", end="")

            # CTR
            elif listing[x] == charsDic["CTR"] \
            or ((x == 2) and (y == 0) and (pos == 2)):
                print(listing[pos], end="\n")

            # BOL
            elif listing[x] == charsDic["BOL"] \
            and ((x == 0) and (y == 1) and (pos == 3)):
                print(listing[pos], sep="", end=" ")

            # ZON
            elif listing[x] == charsDic["ZON"] \
            or ((x == 1) and (y == 1) and (pos == 4)):
                print(listing[pos], end=" ")

            # BOR
            elif listing[x] == charsDic["BOR"] \
            or ((x == 2) and (y == 1) and (pos == 5)):
                print(listing[pos], end="\n")

            # CBL
            elif listing[x] == charsDic["CBL"] \
            or ((x == 0) and (y == 2) and (pos == 6)):
                print(listing[pos], charsDic["BOB"], sep="", end=" ")

            # BOB
            elif listing[x] == charsDic["BOB"] \
            or ((x == 1) and (y == 2) and (pos == 7)):
                print(listing[pos], charsDic["BOB"], sep="", end="")

            # CBR
            elif listing[x] == charsDic["CBR"] \
            or ((x == 2) and (y == 2) and (pos == 8)):
                print(listing[pos], end="\n")
            pos += 1


And then, problems arise: No matter how I try to get the wanted output, there is always a problem, it drives me crazy. In this sample of code, I get this output:

Code:

┏━━━┓
┃ ·━┃
┗━ ━━┛


Which is very wrong.

Let me explain a few things before keep going: since the first and last row were having space between it’s characters (in this case below, ┏ ━ ┓, or I want ┏━━━┓so I repeat some characters on top and bottom line).

As for the zone within the frame, the x dimension was to tight and was resulting in this kind of output:
Code:
┃ · ┃


instead of
Code:
┃  ·  ┃


As a result, a 3×3 frame was looking like a 1×3. To fix that I add a space after all left border and after the ’·’ characters. I also tried to set on a single line a print with all characters, using sep and end options to get the result I wanted.

This is just plainly a pain where I think of. Each time I solve a bug, a new more widely one make a scene.

I know I am the problem here. I think after 2 or 3 days into the same problem I started to become stuck by myself, unable to think out of a box.

I surely miss something with my conditional-statement. As you can see, there is a mix of ’or’ as ’and’ between the condition if / elif. I just noticed that some condition needed ’and’ after the first if/elif, some ’or’. I even added () around math to try to make it more obvious to me.

Another note about the usage of ’pos’ variable: It allows me to get the actual position inside the list, because I had this problem with "BOT" and "BOB" being the same value, or "BOL" and "BOR" also having the same characters (from another key), it was really making my printing incorrect. Adding more condition (checking actual value for x, y plus pos) allowed me to properly, almost, know at each iteration where am I.

The main file is running the whole in a loop to test different size:

Code:
def main():

    if __name__ == "__main__":
        main()

# Ask for lenght & height here
lenght = 3
height = 3

# # # Code execution # # #
listing = orders_frame_perimeter(lenght, height)
print(listing)
for i in range(3, 11, 1):
    lenght, height = i, i
    listing = orders_frame_perimeter(lenght, height)
    print_frame(listing, lenght, height)


3, 4, 11 giving only a single printing, etc.

So… I know all this code is pure crap, I do not want to learn at the moment the usage of object-class, beside I know I am actually manipulating them only by using python, but after some try in other piece of code I prefer for now to not add some weight in my learning and keep focusing on a more «script» or «procedural» ways for now.

Have you a tips, guidance, anything to help me understand what is wrong please ?

Regards,
GASPARD DE RENEFORT Kévin
_________________
«Gentoo does not have problems, only learning opportunities.» - NeddySeagoon
«If your Gentoo installation isn't valuable to you, feel free to continue to ignore the instructions.» - figueroa
Back to top
View user's profile Send private message
sublogic
Apprentice
Apprentice


Joined: 21 Mar 2022
Posts: 222
Location: Pennsylvania, USA

PostPosted: Fri Apr 05, 2024 12:46 am    Post subject: Reply with quote

One problem is that charsDic["BOB"] and charsDic["BOT"] are equal, as are charsDic["BOL"] and charsDic["BOR"]. Your chain of if statements can't discriminate within these pairs and always pick the first match.
Edit: in print_frame you probably needed to test listing[pos] instead of listing[x] --but it would still be broken, per above.

Here's a patch, without redesigning the whole thing. It handles xSides correctly but ignores ySides. (But, you know, you should redesign the whole thing... :wink: )
Code:
diff -r 7ac385ca9aac GoTDA_functions.py
--- a/GoTDA_functions.py        Thu Apr 04 18:10:04 2024 -0400
+++ b/GoTDA_functions.py        Thu Apr 04 20:40:48 2024 -0400
@@ -128,48 +128,39 @@
         for x in range(0, lenght, 1):
 
             # CTL
-            if listing[x] == charsDic["CTL"] \
-            and ((x == 0) and (y == 0) and (pos == 0)):
-                print(listing[pos], charsDic["BOT"], sep="", end="")
+            if ((x == 0) and (y == 0)):
+                print(listing[pos], charsDic["BOT"]*(xSides-1), sep="", end="")
 
             # BOT
-            elif listing[x] == charsDic["BOT"] \
-            or ((x == 1) and (y == 0) and (pos == 1)):
-                print(listing[pos], charsDic["BOT"], sep="", end="")
+            elif ((x < lenght-1) and (y == 0)):
+                print(listing[pos]*xSides, end="")
 
             # CTR
-            elif listing[x] == charsDic["CTR"] \
-            or ((x == 2) and (y == 0) and (pos == 2)):
+            elif ((x == lenght-1) and (y == 0)):
                 print(listing[pos], end="\n")
 
             # BOL
-            elif listing[x] == charsDic["BOL"] \
-            or ((x == 0) and (y == 1) and (pos == 3)):
-                print(listing[pos], sep="", end=" ")
+            elif ((x == 0) and (y < height-1)):
+                print(listing[pos], " "*(xSides-1), sep="", end="")
 
             # ZON
-            elif listing[x] == charsDic["ZON"] \
-            or ((x == 1) and (y == 1) and (pos == 4)):
-                print(listing[pos], end=" ")
+            elif ((x < lenght-1) and (y < height-1)):
+                print(listing[pos], " "*(xSides-1), sep="", end="")
 
             # BOR
-            elif listing[x] == charsDic["BOR"] \
-            or ((x == 2) and (y == 1) and (pos == 5)):
+            elif ((x == lenght-1) and (y < height-1)):
                 print(listing[pos], end="\n")
 
             # CBL
-            elif listing[x] == charsDic["CBL"] \
-            or ((x == 0) and (y == 2) and (pos == 6)):
-                print(listing[pos], charsDic["BOB"], sep="", end=" ")
+            elif ((x == 0) and (y == height-1)):
+                print(listing[pos], charsDic["BOB"]*(xSides-1), sep="", end="")
 
             # BOB
-            elif listing[x] == charsDic["BOB"] \
-            or ((x == 1) and (y == 2) and (pos == 7)):
-                print(listing[pos], charsDic["BOB"], sep="", end="")
+            elif ((x < lenght-1) and (y == height-1)):
+                print(listing[pos]*xSides, end="")
 
             # CBR
-            elif listing[x] == charsDic["CBR"] \
-            or ((x == 2) and (y == 2) and (pos == 8)):
+            elif ((x == lenght-1) and (y == height-1)):
                 print(listing[pos], end="\n")
             pos += 1
 


Last edited by sublogic on Fri Apr 05, 2024 9:12 pm; edited 1 time in total
Back to top
View user's profile Send private message
Genone
Retired Dev
Retired Dev


Joined: 14 Mar 2003
Posts: 9535
Location: beyond the rim

PostPosted: Fri Apr 05, 2024 9:25 am    Post subject: Reply with quote

I'd say your code is overcomplicated if your goal is to only draw a rectangular ASCII frame, which of course makes it hard to understand why things aren't working as expected.

Consider rewriting so that instead of having one huge if-elif block inside your nested loop you have separate loops for the upper and lower borders. That should greatly simplify your conditionals and make it easier to isolate any problems. Don't need to learn anything new for that.
Back to top
View user's profile Send private message
kgdrenefort
Apprentice
Apprentice


Joined: 19 Sep 2023
Posts: 186
Location: Somewhere in the 77

PostPosted: Tue Apr 09, 2024 10:09 am    Post subject: Reply with quote

Hello,

Thanks for theses replies, sadly I didn’t get into my code since this topic was made because of numerous gentoo installation I’m doing at the moment while binhosting them.

Will try these and rewrite later too.

Yeah, the code is over complicated, that is not really designed to be used as it is. Just tried to works with functions from another file, getting some experience.

Regards,
GASPARD DE RENEFORT Kévin
_________________
«Gentoo does not have problems, only learning opportunities.» - NeddySeagoon
«If your Gentoo installation isn't valuable to you, feel free to continue to ignore the instructions.» - figueroa
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Portage & Programming All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum