Reputation: 25
I was currently finishing up my project that concerned ciphering a sentence using a key matrix. As of now, all the calculations are done and correct, the only thing left is for me to print the output to the screen and voila.
What you can see above is my current output. The Key matrix is printed just how I want it to be printed and all is good with Encrypted message as well, except that my print prints one space more than what is needed. This is quite problematic because of the fact that the project is going to be verified with a bot, so even though it changes nothing on the screen (except when piping into cat -e) the bot is still going to pick it up.
My print function looks like the following:
def the_output_printer(key_matrix, encrypted_matrix, matrix_size):
length = (len(sys.argv[1]));
if (length == 0):
exit(84);
lines = round(length / matrix_size, 0);
if (length % matrix_size != 0):
lines += 1
lines = int(lines);
print("Key matrix:");
for line_index_key in range(0, matrix_size):
for col_index_key in range(0, matrix_size):
if (col_index_key != matrix_size - 1):
print("{}".format(key_matrix[line_index_key][col_index_key]), end="\t");
if (col_index_key == matrix_size - 1):
print("{}".format(key_matrix[line_index_key][col_index_key]), end="\n");
print("\n", end="");
print("Encrypted message:")
for line_index in range(0, lines):
for col_index in range(0, matrix_size):
print("{}".format(encrypted_matrix[line_index][col_index]), end=" ");
print();
I tried something simillar to what I've done for Key Matrix but it didn't really work out. Any ideas as how to not print that last " "?
Thank you all for reading!
As I'm still new to python please excuse my horrible coding style, I'm still working on it.
Upvotes: 0
Views: 63
Reputation: 25
Alright, I figured it out :)! The problem was that as I'm still new to Python, I once again messed up how range() functioned. My previous attempt at fixing this resembled:
if (line_index == lines and col_index == matrix_size):
print("{}".format(encrypted_matrix[line_index][col_index]));
else:
print("{}".format(encrypted_matrix[line_index][col_index]), end=" ");
I ommited the fact that while using
in range(foo)
the value stops at foo - 1, or quite simply while it is strictly inferior to foo. The fix was quite simply to add -1 to my if statement, EXACTLY the same thing I forgot when i was doing my key_matrix printing part. The fixed part of the function now looks lice this:
if (line_index == lines - 1 and col_index == matrix_size - 1):
print("{}".format(encrypted_matrix[line_index][col_index]));
else:
print("{}".format(encrypted_matrix[line_index][col_index]), end=" ");
Which gives me the correct output :). Thank you everyone but I figured it out after re-reading the code few times.
Upvotes: 0
Reputation: 37319
Your problem is that each iteration of your nested for
loop prints your end
argument, one space. That's fine assuming you want one between all elements, but it doesn't know to skip it on the last iteration of the nested loop.
There's more than one way to solve this, but because you mentioned being new to Python I'll stick to a simple one - instead of printing each number, collect up the numbers in a single list and then pass that list to print()
. print()
defaults to adding a separator between its arguments but not after the final one.
message_elements = []
for line_index in range(0, lines):
for col_index in range(0, matrix_size):
message_elements.add("{}".format(encrypted_matrix[line_index][col_index]))
print(*message_elements)
The *
operator in the final print()
unpacks the list, as if you'd specified each of its elements as an argument to print()
. It's equivalent to:
print(message_elements[0], message_elements[1], ...)
Once you're more experienced with Python there are ways to more concisely collect up the matrix elements into a list, or to avoid an intermediate list at all and use a single expression to do it. But this should work and changes as little as I can from your existing code.
Upvotes: 1