He took a deep breath and managed to reign in his panic, if just barely. “Great job Baldric, look what you’ve got yourself into now. You just had to join up with a rebel and navigate an underground labyrinth to slay a bureaucratic abomination. Couldn’t have just called it a day. Nope, that would’ve been too easy. Sigh, I’m too old for this sort of thing…” He paused and shook his head. “No, it’s not over yet. I’ll solve this problem just like all the others, one step at a time. Hmm, what to do…first let’s see how sturdy these bars are.”
He attempted to cast pure Fire at the bars, but found he couldn’t channel a drop. It was as if there was a barrier between him and that endless well of Magic he had previously been tapped into. Surely they couldn’t take away someone’s ability to use Magic, could they? Panic rose up inside him again. He reached toward his bonds with his teeth, thinking maybe he could at least gnaw his way off of the table. That was when he noticed numbers on the ground.
He strained his neck trying to get a better look and saw circles of faint green glowing numbers surrounding him, magically inscribed into the floor of his cell. Two concentric circles, in fact, but he couldn’t see all the numbers from his position. He attempted to weave Logic to probe the mystical patterns, and found to his surprise that he could still channel that element at least. With a strand of Logic he mentally scanned the circles and found that each consisted of a sequence of 13 numbers.
1 2 |
>>> outer_sequence = [13, 19, 29, 37, 43, 53, 61, 71, 79, 89, 101, 107, 113] >>> inner_sequence = [17, 23, 31, 41, 47, 59, 67, 73, 83, 97, 103, 109, 127] |
Baldric couldn’t say why, but the outer circle felt like it needed to be added together, and the inner circle felt as if it needed to all be multiplied together. Decidedly odd, but he was fairly certain about it.
“For this outer sequence, I suppose I could just manually add the numbers, 13 + 17 + 19… and so on, but that would be tedious, and this looks like a good opportunity to finally use some loops. And specifically in this case, for
loops, which, as I recall, cycle through each object in a series of objects.”
“I’d better test them out real quick first. Let’s see, what if we just have a list of numbers and want to print them all out one by one?”
1 2 3 4 5 6 7 8 9 |
# note how the contents of a for loop, after the first line, must be indented >>> for num in [0, 1, 2, 3, 4]: print(num) 0 1 2 3 4 |
“Or what if we have a string and want to print each individual character?”
1 2 3 4 5 6 7 8 9 10 11 12 |
>>> for char in "abcde": print(char) a b c d e # note that the variable name 'char' is arbitrary # you can use whatever name you want there # as long as you correctly refer to it inside the loop |
“Excellent. Well, lists and strings work with for
loops. Maybe integers or floats do too. Or booleans, for that matter.”
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
>>> for something in 4: print(something) Traceback (most recent call last): File "python", line 1, in TypeError: 'int' object is not iterable # got an error looping on an integer >>> for something in 4.2: print(something) Traceback (most recent call last): File "python", line 1, in TypeError: 'float' object is not iterable # another error for a float >>> for something in True: print(something) Traceback (most recent call last): File "python", line 1, in TypeError: 'bool' object is not iterable # and yet another error for a boolean |
“Hmm, integers, floats, and booleans aren’t ‘iterable’, but lists and strings are. Interesting. Anyways, it looks like I should be able to use a for
loop and add up all the numbers in the outer circle, one at a time.”
1 2 3 4 5 6 7 8 9 |
outer_total = 0 # initialize the variable we'll add everything to # iterate over each number in a list of numbers and add them all up, one at a time for number in outer_sequence: outer_total += number # add a print statement here if you want to see what's happening at each step # print(outer_total, number) print(outer_total) |
1 |
815 |
“Now for the inner sequence, where I’m pretty sure I need to multiply all the numbers together. Nearly identical Magic, I just need to change the starting total to 1 and the operation to multiplication.”
1 2 3 4 5 6 |
inner_total = 1 for number in inner_sequence: inner_total *= number print(inner_total) |
1 |
77372585679822157429417 |
“And now for breaking this thing! Probing the seal, it feels as if…I need to combine these results in some way, probably by multiplying them together. But there’s something else…I think it might need my HR ID number too.”
1 2 |
answer = outer_total * inner_total * 601 print(answer) |
1 |
37898253054762090041289887855 |
As soon as Baldric’s Logic printed out the answer, the circles began to dissolve. One by one, starting with the outer sequence, the green glowing numbers evaporated until the entire seal was gone. Baldric attempted to weave Fire, and elemental power surged through him. It had worked!
A door creaked open from down the hall, then crashed closed. Footsteps were approaching, a confident, relaxed stride.
A wave of dread washed over Baldric. He immediately channeled Fire at the ropes holding him to the table and stood up. “I need to break out of here, I won’t let them review my performance! But I need to think, how to escape…I know, I’ll make a key!” He visualized a key that would fit perfectly into his cell door’s lock, then wove Earth into the keyhole, feeling it out, and let the weaves solidify when he thought he had the shape right. He rushed over to turn the key, but it didn’t budge. The key had fused to the keyhole! The footsteps began to hurry, and Baldric backed away from the front of his cell.
“Deep breaths, there has to be a way out of this. Well, if I can’t open the door…I’ll just melt it!”
He channeled pure Fire at the bars of his cell. Nothing happened. He channeled more, strained to pour as much Fire as he could draw on straight into the iron. The metal began to glow, soft at first, then brighter, until it illuminated the hallway, but they didn’t melt. That’s when he saw Cassandra stroll in front of his cell in all her icy beauty, smiling.
“So you were the Emperor’s pawn all along,” Baldric grumbled. “Do your worst, I’m not afraid of the Empire anymore lady. Someday this bureaucratic nightmare will crumble and I’ll be laughing all the way to dwarven heaven when you and your cronies…”
“That day may come sooner than you think,” replied Cassandra, still smiling. She held out her hand, and Baldric saw thick flows of Wind and Water being woven in extremely complex patterns. It looked like Ice Magic. Frantically, Baldric began preparing his own Fire and Earth-based counterattack. “I’m warning you, if you don’t think I’ll…” Baldric trailed off as Cassandra directed her Ice flows at the cell bars, which glowed a deep arctic blue. Baldric shivered at the sudden drop in room temperature. Then she wove a club of Wind and smashed it straight into the iron bars, shattering them like glass. Baldric shielded his face from the frozen metal shards.
“My apologies for earlier Baldric, there was no other way. I didn’t expect you to cause such a commotion with the HR Manager, and events have been moving faster than I can keep up with. How did you manage to defeat the beast? I knew you had a natural talent for Magic, for Logic at least, but obliterating the creature like that…”
“It wasn’t me, I just helped out a little. I met someone named Geofram Hammerfell on my way the Manager’s office.”
“So the Hammer of the Light still lives…there may be hope yet. Where did he go?”
“Not sure, he didn’t say much. But Cassandra, I don’t get it. Are you an Imperial or not? You were with all those Managers, and they wouldn’t be fooled by just any rebel.”
“I’m a defector, Baldric. I was in charge of the Imperial Secret Police, but realized some time ago that the Empire wasn’t what I thought it was. It’s evil to its core Baldric, evil almost beyond comprehension, and something needed to be done about it. Remember the Emperor’s mysterious power source I told you about? It is locked behind an enchanted Door that can only be opened with powerful Logic, far too powerful for me to handle. I needed to find a rebellion-minded Logic user, and that’s where you came in.”
“The Emperor somehow has the power to not only sense but pinpoint Magic use in the city, and when you began channeling he sent me after you. Searching your home, we found evidence you clearly possessed rebellious leanings, but I still needed to find out if you were strong enough in Logic for the task ahead. That request you got at work was from me. It was a test to ensure your powers of Logic were sound, but it was also part of the Logical puzzle whose solution will open the Emperor’s Door. I solved the first part of the puzzle by hand, just shifting every letter by two, which yielded the instructions in the letter. The number you answered with somehow relates to the second section of the puzzle, though I’m not sure how.”
“Then of course I sent you to fetch the Teleportation Circle key in the HR Manager’s lockbox.” Cassandra pulled out a pale green crystal from her pocket and handed it to Baldric. “The Emperor possesses a key as well, but the HR Manager’s was much less risky to go after. Meanwhile I myself, with the help of some scrolls, teleported to the rebel base in the mountains to the south, spoke to them while under a truth-telling spell, and convinced that this plan of mine was their last hope. My two half-elf assistants, who you may have noticed previously, are still with them, briefing them on the layout of the city and defenses of the castle. Once we free the prisoners here, we’ll proceed to the Teleportation Circle, which you’ll need to unlock with the help of that key. Then we’ll transport the rebel army here and move on the Emperor himself.”
Baldric just stood there, trying to process everything he’d just heard.
“In any case, we must move with all haste Baldric. The Emperor is planning something big, something called “Plan Z”, which according to my sources is some sort of ultimate budget-cutting measure. Rumor has it they’re saving up to remodel the whole empire according to an open floor plan. I don’t know what that means, but it won’t be good. So Baldric, I need you to free the prisoners. All of them. You’re the only person I know with enough talent for Logic to do it.
“Each cell block has a different method of sealing. This is cell block A, which has 600 prisoners, you being the latest addition. B has 800 prisoners, and C 1,000. Many of them are rebels and magic users that we will need to complete our plan, though of course some are just here for infractions like being late to work or thinking critically. Regardless, we must free them quickly before the Emperor is alerted to our actions. I’ve taken care of some of the guards already, but more will be coming.”
Cell Block A
The pair left the torture room and Cassandra led him through several storerooms containing large quantities of whips, corpse shrouds, and impaling stakes. They soon came to a large open 3-tiered cell block with catwalks along the perimeter of each tier and staircases connecting them. They were on the top tier, and moving towards the railing of the walkway on this level they got a view of most of the block. Scattered around it were ice sculptures that strongly resembled guards. The cells had no doors or locks of any kind, the Magic seals alone apparently being enough to hold the prisoners on their own. Each cell had a number over it, from 1 to 600.
“I’ll be exhausted long before I can weave enough Logic to break 600 seals. But actually, every seal I can see here looks just like mine did. I’ll bet I can make a function to speed things up, since the same Logic will be needed every time. The only thing that changes is the ID of the prisoner, so that’s the argument that the function will take.”
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
outer_sequence = [13, 19, 29, 37, 43, 53, 61, 71, 79, 89, 101, 107, 113] inner_sequence = [17, 23, 31, 41, 47, 59, 67, 73, 83, 97, 103, 109, 127] def block_A_seal_breaker(id_number): outer_total = 0 for number in outer_sequence: outer_total += number inner_total = 1 for number in inner_sequence: inner_total *= number answer = outer_total * inner_total * id_number return answer |
With his function completed, Baldric began running from cell to cell, casting his function. He started with the first cell, which contained prisoner #1, and went from there.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
>>> print(block_A_seal_breaker(1)) 63058657329055058304974855 >>> print(block_A_seal_breaker(2)) 126117314658110116609949710 >>> print(block_A_seal_breaker(3)) 189175971987165174914924565 >>> print(block_A_seal_breaker(4)) 252234629316220233219899420 >>> print(block_A_seal_breaker(5)) 315293286645275291524874275 |
After releasing the fifth prisoner and hearing their incredulous cries of joy, Baldric had to stop and lean against the wall to catch his breath. “There’s no way I can do this for every cell, there’s too many. There’s got to be a better way…wait, what if I used another for
loop? For every ID number, I call my function on that ID! I’ll just need to make a list of the numbers to iterate over, from 6 to 600. And I think I should be able to make that list with a while
loop.”
1 2 3 4 5 6 7 8 9 10 |
id_list = [] # initialize empty list id_number = 6 # prisoners with ID's 1-5 freed already, so start with #6 # while loops keep going until their condition is met (or forever if condition is never met) # this one keeps on going until id_number gets to 601 while id_number <= 600: id_list = id_list + [id_number] # add next id_number to list id_number += 1 # increment id_number, otherwise while loop will never stop print(id_list) |
1 |
[6, 7, 8...598, 599, 600] |
“And now I can iterate over that list and plug all those ID numbers into my seal breaker function.”
1 2 3 |
# call the seal breaker function on every id number in the list and print the result for id_number in id_list: print(block_A_seal_breaker(id_number)) |
1 2 3 4 5 6 7 8 9 |
378351943974330349829849130 441410601303385408134823985 504469258632440466439798840 . . . 37709077082774924866374963290 37772135740103979924679938145 37835194397433034982984913000 |
The seal-breaking number was rapidly printed for one cell after another, and all the prisoners on that block were released from their magical confines almost instantaneously. Baldric was surprised by a low, rumbling voice behind him.
“You make mistake dwarf.”
Baldric turned around to see the biggest half-orc he had ever seen standing behind him, arms crossed.
“If you add one thing at a time to a list, you should use append()
list method.”
Baldric saw the half-orc weave his own Logic. The flows were weak but precise. Perhaps this half-orc didn’t have much power with Logic, but he knew what he was doing.
1 2 3 4 5 6 7 8 9 10 11 |
id_list = [] id_number = 6 while id_number <= 600: id_list.append(id_number) # here's the change in the code id_number += 1 print(id_list) # notice there was no variable assignment to the list when using append() # more on this next chapter |
1 |
[6, 7, 8...598, 599, 600] |
“But it is still stupid. No need for a while
loop. Just use built-in range()
function. Works like this.”
1 2 |
for num in range(0, 5): # also try leaving out the first argument, range(5) print(num) |
1 2 3 4 5 |
0 1 2 3 4 |
“And for your spell, it will look like this.”
1 2 |
for id_number in range(6, 601): print(block_A_seal_breaker(id_number)) |
“So now whole seal-breaker looks like this:”
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
outer_sequence = [13, 19, 29, 37, 43, 53, 61, 71, 79, 89, 101, 107, 113] inner_sequence = [17, 23, 31, 41, 47, 59, 67, 73, 83, 97, 103, 109, 127] def block_A_seal_breaker(id_number): outer_total = 0 for number in outer_sequence: outer_total += number inner_total = 1 for number in inner_sequence: inner_total *= number answer = outer_total * inner_total * id_number return answer for id_number in range(6, 601): print(block_A_seal_breaker(id_number)) |
1 2 3 4 5 6 7 8 9 |
378351943974330349829849130 441410601303385408134823985 504469258632440466439798840 . . . 37709077082774924866374963290 37772135740103979924679938145 37835194397433034982984913000 |
“But if you ever do use a while
loop, just make sure the loop finishes. Infinite loops are bad juju.”
Baldric pondered the half-orc’s Logic. “Hmm, built-in functions and methods…they were mentioned in that Book, but I don’t remember much about them.”
Cassandra walked up to Baldric. “Perhaps you can chat with your new friend after we’ve freed the prisoners, hmm?”
“Yeah yeah, I’m on it. What was your name, friend? I’m Baldric.”
“Doomboom. You free prisoners, I smash Emperor.”
Baldric extended his hand, but regretted it even before that massive greenish hand engulfed his own. He thought he could hear his bones creaking.
The prison erupted in chaos. Several crashes sounded simultaneously throughout the cell block, followed by streams of armed Imperial guards. Several rebels fell immediately to flurries of arrows, but soon elemental projectiles were flying in the opposite direction. The screams of the wounded and dying mixed with the crashes and explosions of lethal spells.
“Baldric, we need to get to Cell Block B, now!” Cassandra shouted over the din. He followed Cassandra along the walkway to a heavy iron-reinforced door. She tried opening it with a flow of Wind, but it didn’t budge. Guards were flooding in from another door on this level and heading toward them. Baldric and Cassandra were both readying spells when Doomboom appeared from behind. To Baldric’s suprise, it looked like the half-orc was actually channeling Magic directly into himself! Doomboom’s muscles bulged and in one smooth motion he yanked the door off its hinges and hurled it like a frisbee of death at the oncoming guards, sending them all flying backwards or over the railing. Doomboom looked around for a moment then leapt over the railing himself to join the battle below.
Cassandra looked impressed for only a moment, then shook herself and shouted, “Through here, hurry!” Baldric rushed through the door and into Cell Block B.
Cell Block B
This cell block was much like the last, but one story deeper. And unlike the last one, guards were already waiting and launched volleys of arrows at them as soon as they were through. Cassandra wove a spell faster than Baldric had thought possible, and a wall of ice rose around them just in time to intercept the arrows. She channeled more, and the wall of ice broke down the center and each half went flying towards a different group of guards. This was followed by a barrage of ice spears that impaled guards left and right. Guards elsewhere in the cell block were getting in position for more volleys, and with another of Cassandra’s spells the floor became slick with ice, sending them slipping and falling over each other. Without pausing in her channeling, she shouted over her shoulder, “What are you waiting for Baldric? Get these people free!”
Baldric rushed over to the closest cell, this one containing a gaunt, hopeless-looking halfing, and examined the seal. Again, there were two circles of numbers, each composed of the same numbers as in Cell Block A. But he could sense that different calculations were required for this one: the outer circle numbers needed to all be multiplied together again, but only the first digit of each number. And the inner circle numbers also needed to be multiplied together, but only the last digit of each number.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
outer_sequence = [13, 19, 29, 37, 43, 53, 61, 71, 79, 89, 101, 107, 113] inner_sequence = [17, 23, 31, 41, 47, 59, 67, 73, 83, 97, 103, 109, 127] def block_B_seal_breaker(outer_sequence, inner_sequence, id_num): outer_total = 1 for num in outer_sequence: first_digit = str(num)[0] # convert number to string, select first digit outer_total *= int(first_digit) # convert str back to int and multiply into total inner_total = 1 for num in inner_sequence: last_digit = str(num)[-1] # convert number to string, select last digit inner_total *= int(last_digit) # convert str back to int and multiply into total return outer_total * inner_total * id_num for id_num in range(1, 801): print(block_B_seal_breaker(outer_sequence, inner_sequence, id_num)) |
1 2 3 4 5 6 7 8 9 |
31122809988480 62245619976960 93368429965440 . . . 24836002370807040 24867125180795520 24898247990784000 |
Again the seals were shattered throughout the cell block, and once again the prisoners rushed out to take up the fight. A guard about to loose an arrow at Baldric was tackled by a familiar looking person. It was Steve from the office!
Cell Block C
Seeing Cassandra was still occupied, Baldric headed by himself towards the door to Cell Block C. He intuitively channeled a combustive mix of Wind and Fire mid-stride and blew the door open as he ran through. There weren’t any guards in here thankfully, just 1,000 more prisoners in varying states of despair. He inspected the seal in the first cell he came to and found the same sequences of numbers in use once again. But this time he sensed that the trick to breaking the seals was somewhat different.
It felt like he needed to loop through both sequences at the same time, concatenate the elements from each list together (i.e. 13 and 17 become 1317) and then multiply all these numbers together. And just like the last two times, that final product needed to be multiplied by a given prisoner’s ID number for the seal to be broken. But the question remained, how could he loop through two loops at the same time? A voice, as if in response to his thoughts, rumbled behind him.
“You need to loop through two lists. This is one way. I will show you a better way later.”
1 2 3 4 5 6 7 |
outer_sequence = [13, 19, 29, 37, 43, 53, 61, 71, 79, 89, 101, 107, 113] inner_sequence = [17, 23, 31, 41, 47, 59, 67, 73, 83, 97, 103, 109, 127] # use the built-in len() function to calculate the length of the lists # note you could use length of either list since they have equal length for i in range(len(outer_sequence)): print(outer_sequence[i], inner_sequence[i]) |
1 2 3 4 5 6 7 8 9 10 11 12 13 |
13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97 101 103 107 109 113 127 |
Baldric pondered the Logic. “Hmm, another built-in function, len()
. Those are starting to seem pretty useful. Anyways, I need to break these seals, and fast, before Imperial reinforcements show up.”
<<< Chapter 4 – FunctionsChapter 6 – Built-in Functions and Methods >>>
Leave a comment