博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
python循环1-31_Python简介,第5章-循环
阅读量:2519 次
发布时间:2019-05-11

本文共 19127 字,大约阅读时间需要 63 分钟。

python循环1-31



Baldric groggily opened his eyes and was greeted by a splitting pain in his head. He had been having the strangest dream, something about a crystal, and a snake eating its own tail, and…a singing head of cabbage? Strange indeed. He looked around in the dim torchlight and found himself in a large cell of some sort, gray stone on three sides and thick iron bars on the fourth. The room was filled with all kinds of contraptions of wood and metal, many with straps or chains or hooks. He was glad he didn’t know what they were for, and had no intention of finding out. Trying to move, he found his arms and legs were tied down to a table. He also saw he was garbed in a dreary gray jumpsuit with the number 601 stitched on the breast. The sounds of distant screams reverberated off the walls, and terror surged through Baldric’s veins as he realized where he was. Humanoid Resources.

aldric groggily睁开了眼睛,并在他的头部分裂的痛苦招呼。 他一直在做着最奇怪的梦,关于水晶的事,还有一条蛇正在吞噬自己的尾巴,还有……唱歌的白菜头? 确实很奇怪。 他在昏暗的手电筒中环顾四周,发现自己身处某种巨大的牢房中,三面是灰色石头,四面是厚铁棍。 房间里摆满了各种木头和金属的装置,许多都带有皮带,链子或钩子。 他很高兴自己不知道它们的用途,也不想找出来。 试图移动时,他发现胳膊和腿被绑在桌子上。 他还看到自己穿着沉闷的灰色连身衣,胸前缝着601号衣服。 远处的尖叫声在墙壁上回荡,当鲍德瑞克意识到自己在哪里时,恐惧在他的血管中蔓延。 人形资源。

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 getting too old for this.” 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. 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.

他拉紧脖子,试图使自己看起来更好,并看到周围环绕着淡淡的绿色发光数字,神奇地刻在他的牢房地板上。 实际上,有两个同心圆,但是他看不到自己位置上的所有数字。 他试图编织逻辑以探究神秘的模式,但令他惊讶的是,他至少仍可以引导该元素。 他用一连串的逻辑思维地扫描了圆圈,发现每个圆圈都由13个数字组成。

>>> 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.

Baldric不能说为什么,但是外圈感觉像需要将其加在一起,而内圈感觉好像需要将所有相乘在一起。 固然奇怪,但他对此相当确定。

“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 test out loops. And specifically in this case, for loops, which, as I recall, cycle through each object in a series of objects.”

“对于这个外部序列,我想我可以手动添加数字13 + 17 + 19…等等,但这将是乏味的,这似乎是一个很好的机会来测试循环。 特别是在这种情况下, 对于for循环,据我记得,它循环遍历一系列对象中的每个对象。”

“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?”

“我最好先快速地对它们进行测试。 让我们看看,如果我们只列出一个数字并想一一打印出来呢?”

# 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)01234

“Or what if we have a string and want to print each individual character?”

“或者如果我们有一个字符串并想打印每个字符怎么办?”

>>> for char in "abcde":        print(char)abcde# 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.”

“优秀的。 好吧,列表和字符串可用于for循环。 也许整数或浮点数也可以。 还是布尔值。”

>>> 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.”

“嗯,整数,浮点数和布尔值不是'可迭代的',但是列表和字符串是。 有趣。 无论如何,看起来我应该能够使用for循环并一次将一个外圈中的所有数字相加。”

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 timefor 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)
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,将运算更改为乘法。”

inner_total = 1for number in inner_sequence:    inner_total *= numberprint(inner_total)
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.”

“现在要打破这件事! 探测封印,似乎……我需要以某种方式组合这些结果,可能是将它们相乘。 但是还有其他事情……我认为它可能也需要我的HR ID号。”

answer = outer_total * inner_total * 601print(answer)
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!

鲍德瑞克的《逻辑》一经印出答案,圈子就开始瓦解。 从外部序列开始,一个接一个的绿色发光数字逐渐消失,直到整个印章都消失了。 鲍德瑞克(Baldric)试图编织火焰,元素力量在他身上激增。 它奏效了!

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.

一波可怕的光芒笼罩着波德力克。 他立即用火将绳索绑在桌子上,站了起来。 “我需要突围,我不会让他们评估我的表现! 但是我需要思考,如何逃脱……我知道,我会一把钥匙!” 他形象地看到一把钥匙,该钥匙可以完全适合他的牢房门锁,然后将地球编织到钥匙Kong中,感觉到它的存在,并在他认为形状正确时让编织固化。 他冲过去把钥匙转了一下,但是没有动弹。 钥匙已经锁在钥匙Kong里了! 脚步开始急促,鲍德瑞克从牢房的前面退后了。

“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.

他将纯净的火焰引导到牢房的栅栏上。 什么都没有发生。 他引导了更多,竭尽全力将尽可能多的火直接倒入铁杆中。 金属开始发光,先是柔软,然后变亮,直到照亮了走廊,但它们没有融化。 从那时起,他看到卡桑德拉(Cassandra)带着冰冷的美丽在他的牢房前漫步,微笑着。

“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.

“那一天可能比您想象的要早,”卡桑德拉仍然微笑着回答。 她伸出手,鲍德瑞克看到浓密的风水交织成极其复杂的图案。 看起来像冰魔法。 疯狂地,鲍德瑞克开始准备自己的“火与地”反击。 “我警告你,如果你不认为我会……” Baldric走了下来,Cassandra将她的冰流引导到细胞条上,发出深深的北极蓝色。 鲍德瑞克在室温突然下降时发抖。 然后,她编织了一个风之棍,并将其直接砸入铁条,像玻璃一样粉碎它们。 鲍德里克用冰冻的金属碎片遮住了脸。

“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…”

“我为早先的鲍德瑞克(Baldric)道歉,没有其他办法。 我没想到您会与HR Manager引起如此大的骚动,而且活动进展的速度超出了我的能力。 您如何击败野兽? 我知道你至少对魔术有天赋,但对逻辑却如此,但是却消灭了这种生物……”

“It wasn’t me, I just helped out a little. I met someone named Geofram Hammerfell on my way the Manager’s office.”

“不是我,我只是帮了一点忙。 在经理办公室的途中,我遇到了一个叫Geofram Hammerfell的人。”

“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.”

“我是叛逃者,鲍德瑞克。 我负责帝国秘密警察,但前段时间意识到帝国不是我想的那样。 它对核心Baldric来说是邪恶的,几乎是无法理解的邪恶,因此需要采取一些措施。 还记得我告诉过的皇帝的神秘力量吗? 它被锁在一个迷人的门后面,该门只能用强大的逻辑来打开,这对我来说太强大了。 我需要找到一个具有反叛意识的Logic用户,这就是您的来历。”

“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.”

“然后,我当然给您发送了要在人力资源经理的密码箱中获取Teleportation Circle钥匙的信息。” 卡桑德拉从口袋里掏出一颗淡绿色的水晶,交给了鲍德瑞克。 “皇帝也拥有一把钥匙,但是人事经理的风险要低得多。 同时,我本人在一些古卷的帮助下,被传送到南部山区的叛军基地,在讲真话的时候与他们交谈,并坚信我的这个计划是他们的最后希望。 您以前可能已经注意到的我的两个半精灵助手仍然在他们身边,向他们介绍了城市的布局和城堡的防御设施。 一旦我们释放了这里的囚犯,我们将进入Teleportation Circle,您需要在该钥匙的帮助下进行解锁。 然后,我们将把叛军运送到这里,并继续使皇帝继续前进。”

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.

“无论如何,我们必须与所有仓促的鲍德瑞克一起行动。 皇帝正在计划一些大计划,称为“ Z计划”,根据我的消息来源,这是某种最终的削减预算措施。 有传言称,他们正在根据开放的平面图来积蓄重塑整个帝国。 我不知道那是什么意思,但这不会很好。 所以,鲍德瑞克,我需要你释放囚犯。 他们全部。 您是我认识的唯一有足够才能让Logic做到这一点的人。

“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.”

每个电池块都有不同的密封方法。 这是牢房A,其中有600名囚犯,您是最新添加的囚犯。 B有800名囚犯,C有1,000名囚犯。 他们中的许多人都是叛军和魔术用户,我们将需要完成我们的计划,当然,其中有些人只是为了违规,例如上班迟到或批判性思考。 无论如何,我们必须在皇帝被告知我们的行动之前Swift释放他们。 我已经照顾了一些警卫,但还会有更多警卫到来。”

单元格A (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.

两人离开了酷刑室,卡桑德拉带领他穿过几个储藏室,里面藏有大量的鞭子,尸体裹尸布和刺穿木桩。 他们很快来到了一个大型的三层开放式牢房大楼,每层的外围都有走道和连接它们的楼梯。 他们位于最高层,在这一层上走向人行道的栏杆,他们可以看到大部分街区。 周围散布着非常类似于守卫的冰雕。 牢房没有任何门或锁,仅魔术贴显然足以将囚犯自己抱住。 每个单元格上都有一个数字,从1到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.”

“我会精疲力尽,直到能够编织出足以打破600个封条的逻辑。 但是实际上,我在这里看到的每一个印章都和我的一样。 我敢打赌,我可以创建一个函数来加快速度,因为每次都需要相同的逻辑。 唯一改变的是囚犯的ID,这就是该功能将采用的参数。”

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.

随着功能的完成,Baldric开始从一个单元到另一个单元运行,释放了他的功能。 他从第一个囚犯#1的牢房开始,然后从那里走了。

>>> 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
[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.” “And now I can iterate over that list and plug all those ID numbers into my seal breaker function.” # call the seal breaker function on every id number in the list and print the resultfor id_number in id_list:    print(block_A_seal_breaker(id_number)) 378351943974330349829849130441410601303385408134823985504469258632440466439798840...377090770827749248663749632903777213574010397992467993814537835194397433034982984913000 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. 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.” “You make mistake dwarf.” Baldric turned around to see the biggest half-orc he had ever seen standing behind him, arms crossed. 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.” “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. 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.

翻译自:

python循环1-31

转载地址:http://tjqwd.baihongyu.com/

你可能感兴趣的文章
Python——交互式图形编程
查看>>
经典排序——希尔排序
查看>>
团队编程项目作业2-团队编程项目代码设计规范
查看>>
英特尔公司将停止910GL、915GL和915PL芯片组的生产
查看>>
Maven配置
查看>>
HttpServletRequest /HttpServletResponse
查看>>
SAM4E单片机之旅——24、使用DSP库求向量数量积
查看>>
从远程库克隆库
查看>>
codeforces Unusual Product
查看>>
hdu4348 - To the moon 可持久化线段树 区间修改 离线处理
查看>>
正则表达式的搜索和替换
查看>>
个人项目:WC
查看>>
地鼠的困境SSL1333 最大匹配
查看>>
flume+elasticsearch+kibana遇到的坑
查看>>
【MM系列】在SAP里查看数据的方法
查看>>
C#——winform
查看>>
CSS3 transform制作的漂亮的滚动式导航
查看>>
《小强升职记——时间管理故事书》读书笔记
查看>>
Alpha 冲刺(3/10)
查看>>
Kaldi中的Chain模型
查看>>