[Evalutation and Question]Problem with my program.

Before i say something i want you to know that my english isn't perfect. I looked for help on polish forums, but in the end no one cared enough to help me.

Few days ago my programing teacher in school gave me a homework. My job was to make a program that can encipher text, but in order to get the highest grade it cant be something simple like a ceaser shift or Visionere(im pretty sure i spelled it wrong).

I didn't know much about cryptology so I watched some videos about enigma, and created a program that works like it. Actually it doesn't at all, all these two have in common are 3 rotors, and thats it. But i have encountered few problems with it. But before i talk about it I will try to explain how it works. Once again i remind you that my english is not perfect.

So what happens first is you have to set 3 rotors, each rotor have 27 positions(0-26), and each position has assaigned a value (1-27), every value is unique, and there is no 2 positions with the same value assigned.

After you set 3 rotors, you have to remember their positions, beacuse you will need them to decipher the message.

Lets say you set rotors to (0 0 0) and want to encipher the word (abcde). What will happen first is that each letter will become a number (a=1 b=2 c=3 etc).

All values from rotors will be added together, values for rotors set on (0 0 0) look, like this (10 3 1). These will be added together so, 10+3+1=14. And this will be added to the first letter that was turned into number so. 1 + 14 = 15. Now 15 will turn into a letter (1=a 2=b etc.).

But before B is enciphered the first rotor will go from 0 to 1. so the rotors will be set like this (1 0 0) and the values will look like so (3 3 1). And the letter is going to be enciphered the same way buy instead of adding 14 to the letter it will add 7.

When the rotors get to (26 0 0) next they will go to (0 1 0) and then (1 1 0) (2 1 0) etc.

Deciphering works the same way but instead of adding the values it subtracts them.

I'm sure this explanation looks to you like written by 8 years old kid, but
"i cant english very well" if you speak polish i can explain it much better.

Few aditional information:
-It can encipher 26 letters and space.
-Letter after enciphering can become itself
-Letter after enciphering can become a space
-Space after enciphering can become a space, and a letter.
-All other symbols are treated like spaces, and they will be deciphered into spaces.

So it pretty much works like Visioner and ceaser shift, but thanks to rotors program can encipher 19 683 letters uniqly.
But the problem is... it doesn't. My teacher didn't notice it and i got the highest grade but its still bugging me. I checked the code many times, and every time, every part of the code worked fine, but still it doesn't encipher text they way i want.

Now i will explain what the problem is.
Lets try to encipher letter "a" 4000 times with the rotors set on
(0 0 0). The result looks like this.

Well, it looks like someone tried to type something with their forehead. At first, it looks perfect. 4000 letters a, was enciphered into something completly different and it doesn't look like the cipher is repeating. The problem is, the whole cypher is not repeating but parts of it are. It doesnt make any sense to me. Copy it to notepad and search for "ohlmrusfy" (first 9 letters) They repeat like 8 times, but surrounded by different letters and in random intervals. Same thing goes to other parts of the cipher. I'm clueless. Once again, my code is fine. My math seem to be wrong, but i dont know where im wrong.

I tried to add more rotors but it only makes the repetition to appear even more frequently. I would love to give you my code and program, but they are in polish.

And also, im just curious, how good is my program, how hard it would be to break?

Also, i cant speak very well in english but i understund everything. So feel free to write complicated explanations.

@edit
I found this forum simply by searching "Cryptology forum", but now after reading few other threads in here I feel like this may not be the best place to to post this thread. You guys seem to be discussing stuff that is way more advanced than this. But please if you have some spare time, help me with this problem.

I cant belive you figured it out without even looking at the code.
It seems so obvious right now, I cant belive i didn't figured this out myself. You're like a god of math to me right now. Thank you very much.

But, how can i fix it? Now i know why adding more rotors was a bad idea, most of the time they stay at the same position.
I have few other ideas, like making the rotors change their position more frequently, or making the filter that would detect when this situtation occurs, it would fix the problem of "random" repetition but in the end it would make the whole cipher to repeat more frequently.

This is a bit out of my field, so the following may be a bit inaccurate.

I think the problem you have is that although your rotors have 27**3 possible states, they can only produce 27 possible transformations (caesar ciphers with shifts of 0..26). Additionally, each rotor has the potential to reverse the cipher performed by the previous.

Again this is out of my expertise so I can't say how secure this would be in practice (hopefully someone with more cryptoanalysis knowledge will comment) but you could produce better-looking results by increasing the number of possible transformations.

For example, if the algorithm and middle rotor was changed as follows:

I dont' really undestund what you mean. I'm beginner when it comes to algorithms and coding. Seriously, i'm still having problems with using functions, I mean i can use them but i'm still having problems with "Thinking with functions". This program was a real challenge for me, the only reason i doesn't work just like enigma is because i didn't know how to code it.

Anyway, i have an idea for a different way to encipher the text but its impossible to decipher it by substracting the values, so i would have to use something like a reflector that enigma had. But then the cipher would had the same flaw that enigma had, letter could never become itself.

@edit.
I figured out how to write program that emulates enigma. So
I'll write program with 6 rotors. First 3 will work the same way as enigma.
Input -> 1st rotor -> 2nd rotor -> 3rd rotor -> reflector -> 3rd rotor -> 2nd rotor -> 1st rotor -> output

At this point because of the reflector the letter after encryption can never become itself. This is a serious flaw that allowed code breakers break enigma during War World II.

After the text is enciphered with first 3 rotors, it will go to the next 3, however these rotors will do the same transformation as the one i described in the first post.

I thought about my 1st design and i came to the conclusion that this way of encryption is weak and very vulnerable to brute force. However combined with the 3 enigma rotors, it will allow letter to become itself, and in the end it will add 3 additional rotors, which will strenghten the cipher.
So there are going to be 387 420 489 possible settings of the rotors, and the cipher should no longer "randomly" repeat itself. I dont know how fast good pc can check this number of combinations but im sure this is still vulnerable to brute force, but i have other clever idea how to fix it, but i dont really know how to code it, but i'm sure i'll figure it out.