pythong-code

Python - Problem multiple "or" conditions in a if statement for gcode modifier


I am working on a script to modify some gcode for a school project. My programming skills are very poor but I am working on this with the help of the internet, but go stuck on a if statement with 3 different options.

I need for this to select the lines that start with "G0" and "G1" and respect the length condition.

with just 2 conditions is working fine (it creates an output file so I can confirm)

I've tried just adding the 3rd condition as I show bellow, but that retrieves an empty file - so I'm assuming that is not working (and this seems the correct way to do it as far as I researched).

if(cells[0] !="G0" or cells[0] != "G1" or len(cells) < 4):
    return

Complete Function:

class Point:
x = 0
y = 0
feed = 0

def GetPoint(line):
    line = line.rstrip("\n")
    cells = line.split(" ")
    point = Point()


if(cells[0] != "G1" or len(cells) < 4):
    return

if(cells[1].startswith("F")):
    point.feed = float(cells[1].split("F")[1])
    point.x = float(cells[2].split("X")[1])
    point.y = float(cells[3].split("Y")[1])

else:
    point.x = float(cells[1].split("X")[1])
    point.y = float(cells[2].split("Y")[1])

return point

Solution

  • As already answered, you have a condition that will always evaluate to true. If you're wanting to extract all but the G0 and G1 commands (or the length condition) then try this

    if not (cells[0] == "G0" or cells[0] == "G1") or len(cells) < 4:
    

    This way you are checking whether cells[0] contains the G0 or G1 and then inverting the result. This can also be written as

    if cells[0] != "G0" and cells[0] != "G1" or len(cells) < 4:
    

    by De Morgan's Law but it might not be as intuitive.