How to make this code more readable and without so many if statements? Im sure I can make it with some kind of loop but not sure how.

extends Button


func _on_Button_pressed():
	var input_text = get_node("../LineEdit").text
	var input_number = input_text.to_int()
	var pieces_per_person = input_number / 6
	var remainder = input_number % 6
	if remainder == 1:
		get_node("../Label").set_text(str("PIECES   ", input_number)  + "\n" + str("PIECES PER PERSON   ", pieces_per_person) + "\n" + str("REMAINDER   ", remainder) + "\n" + str("1 Worker   ", pieces_per_person + 1)  + "\n" + str("5 Workers   ", pieces_per_person))
		pass
	elif remainder == 2:
		get_node("../Label").set_text(str("PIECES   ", input_number)  + "\n" + str("PIECES PER PERSON   ", pieces_per_person) + "\n" + str("REMAINDER   ", remainder) + "\n" + str("2 Workers   ", pieces_per_person + 1)  + "\n" + str("4 Workers   ", pieces_per_person))
		pass
	elif remainder == 3:
		get_node("../Label").set_text(str("PIECES   ", input_number)  + "\n" + str("PIECES PER PERSON   ", pieces_per_person) + "\n" + str("REMAINDER   ", remainder) + "\n" + str("3 Workers   ", pieces_per_person + 1)  + "\n" + str("3 Workers   ", pieces_per_person))
		pass
	elif remainder == 4:
		get_node("../Label").set_text(str("PIECES   ", input_number)  + "\n" + str("PIECES PER PERSON   ", pieces_per_person) + "\n" + str("REMAINDER   ", remainder) + "\n" + str("4 Workers   ", pieces_per_person + 1)  + "\n" + str("2 Workers   ", pieces_per_person))
		pass
	elif remainder == 5:
		get_node("../Label").set_text(str("PIECES   ", input_number)  + "\n" + str("PIECES PER PERSON   ", pieces_per_person) + "\n" + str("REMAINDER   ", remainder) + "\n" + str("5 Workers   ", pieces_per_person + 1)  + "\n" + str("1 Worker   ", pieces_per_person))
		pass
	elif remainder == 0:
		get_node("../Label").set_text(str("PIECES   ", input_number)  + "\n" + str("PIECES PER PERSON   ", pieces_per_person) + "\n" + str("REMAINDER   ", remainder) + "\n" + str("All Workers   ", pieces_per_person))
		pass
	else:
		pass

Sorry I dont know how to post code properly.

Additionally, you would take all those repetitive expressions out and as consts before the match expressions. Only the case specific stuff is handled in the match branches.

Apart from collapsing the whole thing to much less code, it is also tremendously much better for readability, less error prone, and in case of changes for instance at the scene tree there'd only be one line to change instead of six.

And how should I go about Localization option for PIECES, PIECES PER PERSON, REMAINDER etc if I want to have it in two languages?

    Pretty sure you can just combine the cases where modulo result is non-zero and drive your worker text from the 'remainder' variable. Something like (untested):

    var worker_num = 6
    func _on_Button_pressed():
        var input_number = get_node("../LineEdit").text.to_int() #assuming your not reusing you can chain .to_int()
        var pieces_per_person = input_number / worker_num
        var remainder = input_number % worker_num
        # following bit looked to be the same under all conditions
        var base_txt = str("PIECES   ", input_number)  + "\n" + str("PIECES PER PERSON   ", pieces_per_person) + "\n" + str("REMAINDER   ", remainder) + "\n"
    	
        if remainder == 0:
    	get_node("../Label").set_text(str(base_txt + str("All Workers   ", pieces_per_person)))
        else:
            # use remainder and worker_num - remainder to drive text for else conditions. 
            get_node("../Label").set_text(base_txt + str(remainder, " Workers   ", pieces_per_person + 1)  + "\n" + str(worker_num - remainder, " Workers   ", pieces_per_person))

    Added a small tweak, replacing the hard coded number 6 with a var worker_count makes this approach reusable for any other worker counts. Though would expect this to live outside the button pressed function if it was going to change programmatically.

    Imagine the type casting above is iffy, but other than that think it should work.

    In addition, place this before the function:
    onready var label: Label = get_node("../Label")

    Then use label in place of get_node("../Label").

    Had another think about it when I woke up and realised two things. One, you're situationally using "Worker" and "Workers" which I didn't account for and Two, I don't think I actually technically "Improved efficiency", just reuse/readability.

    To account for both of these points, you could do something utterly disgusting like this:

    var worker_num = 6
    func _on_Button_pressed():
        var input_number = get_node("../LineEdit").text.to_int() #assuming your not reusing you can chain .to_int()
        var pieces_per_person = input_number / worker_num
        var remainder = input_number % worker_num
        # following bit looked to be the same under all conditions
        var base_txt = str("PIECES   ", input_number)  + "\n" + str("PIECES PER PERSON   ", pieces_per_person) + "\n" + str("REMAINDER   ", remainder) + "\n"
        
        get_node("../Label").set_text(base_txt + str(remainder, " Workers   ", pieces_per_person + 1, "\n").replace(str("0 Workers   ", pieces_per_person + 1, "\n"), "").replace("1 Workers   ", "1 Worker   ")  + str(str(worker_num - remainder).replace(str(worker_num), "All ") + " Workers   " + str(pieces_per_person)).replace("1 Workers   ", "1 Worker   "))

    This I suspect at some level might be faster (depends on how GDScript works under the hood so it could even be slower lol, ymmv) as it avoids branching entirely through use of string replacement, but this is where I would argue readability is absolutely more important than performance, so please don't do this. Also, I suspect this could be done with less replaces, but meh, chaining like this is foul to begin with and should be avoided unless you WANT your colleagues to hate you lol.

    Ultimately the original IF statement would likely occur in sub ms times so not sure why you need more performance for something that should not affect frame times. If you were running this every frame it might be worth interrogating, but ultimately string manipulation in an IF statement should in most cases be negligible.

      DanchoLudiq And how should I go about Localization option for PIECES, PIECES PER PERSON, REMAINDER etc if I want to have it in two languages?

      Feel the right answer is to spend some time looking over https://docs.godotengine.org/en/stable/tutorials/i18n/internationalizing_games.html as at a glance there is a better way than the bootleg approach I thought of below, which was:

      I would probably use a dict with my primary language as keys for readability, with the lang_choice variable in a global singleton. Something like this:

      var lang_choice = "en" 
      var localiser = {
      	"PIECES":{
      		"en": "PIECES   ",
      		"fr": "PIÈCES   "
      		},
      	"PIECES PER PERSON":{
      		"en":"PIECES PER PERSON   ",
      		"fr":"PIÈCES PAR PERSONNE   ",
      		},
      	"REMAINDER":{
      		"en":"REMAINDER   ",
      		"fr":"RESTE   ",
      		}
      }
      
      var base_txt = str(localiser["PIECES"][lang_choice], input_number)  + "\n" + str(localiser["PIECES PER PERSON"][lang_choice], pieces_per_person) + "\n" + str(localiser["REMAINDER"][lang_choice]), remainder + "\n"

      Edit: This feels like it would get cumbersome/messy quickly, but surprisingly my bootleg approach is not far off what is recommended here: https://docs.godotengine.org/en/stable/tutorials/assets_pipeline/importing_translations.html#doc-importing-translations
      Seems like translations in general is just a massive hassle lol.

      Bimbam Lol I cant understand this line at all and I have been staring at it for 10-15 mins but its working. Can you explain how this .replace thing is working please?

      get_node("../Label").set_text(base_txt + str(remainder, " Workers   ", pieces_per_person + 1, "\n").replace(str("0 Workers   ", pieces_per_person + 1, "\n"), "").replace("1 Workers   ", "1 Worker   ")  + str(str(worker_num - remainder).replace(str(worker_num), "All ") + " Workers   " + str(pieces_per_person)).replace("1 Workers   ", "1 Worker   "))

        DanchoLudiq Can you explain how this .replace thing is working please?

        SO the docs are here: https://docs.godotengine.org/en/stable/classes/class_string.html#class-string-method-replace

        But to give some context I will use a simpler example:

        "This is a string".replace("string", "word") = "This is a word"

        So the '.replace' function acts on the string object to the left of it, replacing the instances of the first parameter passed to it (in this case "string") with the second parameter ("word") as a new string object.

        In my example I used the fact the output is also a string object to chain multiple replaces together, e.g:

        "This is a string".replace("string", "word").replace("word","book") = "This is a book"

        I account for when things like "1 workers" should be "1 worker" or when a line is redundant I replace it with "" but I do it all in one command (which is ugly). This replaces the IF block entirely as in most cases the output is the same, and predictable when not. It is probably confusing because I am not just passing a typed string as parameters to replace, but rather programmatically defined strings.

        Like I said this is foul/not readable and I would not recommend it unless it quantifiably improves performance in a way that is actually necessary. I suspect it doesn't/isn't, so always favor readability first as it makes code easier to revisit down the track or if others need to review/modify it.