程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Let python fly: Visually understand python conditional statements, loop structures, random modules, time modules, boolean types

編輯:Python


Day04 Captain Jack Sea Treasure Hunt
Captain Jack drives a cruise ship to search for treasures at sea. There are three groups of seahorses in front of him to lead the way. Captain Jack can choose one of them by judgment.The first group has only one male seahorse, and it only issues one condition (Format 1: if ...).The second group consists of a male seahorse and a female seahorse, each with an equal condition (Format 2: if ... else ...).The third group consists of multiple male seahorses and one female seahorse, which are arranged in grades, and the conditions are set out in order from top to bottom, if one is satisfied (Format 3:

ifConditional expression 1:
Statement block 1
elif Condition expression 2:
Statement block 2
elif Condition expression 3:
Statement block 3

else:
Statement block n
②).
Captain Jack chose the second group, fulfilling its conditions.So, the seahorse took the captain and the cruise ship to a terraced waterfall in the sea (loop structure: classification: while, for ... in ...), The seahorse told the captain that there are manyYou can take gold coins, but you must follow the rules, you can only take up to 10.I will give you 1 gold first(①.Initialization condition (execute once)), You drive the boat to the second floor, where there is an old man with a white beard(②.loop condition (may execute one or more times), he will check your gold, if it reaches 10, you can't go any further.After you go down, you may encounter male seahorses and female seahorses again. They will ask you various conditions and requirements. After you meet them, just follow what he said. (④loop body (maybeExecute 0 or more times)).After reaching the bottom of the sea, you can only get 1 gold coin at a time (③. Iteration condition (may be executed 0 or more times)), After getting it, write it on the wall at the bottom of the seaYour name and the wealth you got, the spring plate will bounce you to the old man with the white beard, he will check your gold coins again, you go back and forth like this, and after you finally get 10 gold coins, you can leave through the underwater tunnel.But if you are too greedy to deceive the white-bearded old man, and every time you take out less than 10 gold coins, you will enter an infinite loop, then only the break knife can save you (infinite loop (Infinite loop): Infinite loop is very resource-intensive, and we need to avoid it; but if it is used with keywords such as break, it will become meaningful; format: while True: loop body), ButThat way, you'll never get a gift for a female seahorse.
Captain Jack listened to the instructions of the seahorse and successfully passed the old man with the white beard and entered the deep sea. He also found two very magical treasures in the sea, one was a colorful Rubik's cube (the beginning of the random moduleExperience: import random
Step 2: Complete the requirements through the module name. function random.randint(m,n) Function: randint(m,n) function, returns a random integer value of [m,n]), anotherOne is the time stopper (the first step: import the time module import time The second step: pass the module name. The function completes the requirement time.sleep(sec) function: sleep(sec) function, once the program executes to the sleep(sec) function position, it will immediately sleep for sec seconds, and when the time is up, it will automatically wake up and continue to execute).The colorful cube can automatically turn out the number you want, and the time stopper can make the world sleep.Captain Jack put them in his pocket.
Captain Jack continued down, and sure enough he came across the male seahorse, this male seahorse is above a cage, it will ask you questions, if you answer correctly, the cage will take you from the bottom againGo to the hippocampus and continue answering questions.If you get it wrong, you can exit through the small door at the bottom of the basket.Captain Jack successfully passed after being transferred twice (for continue: if the continue keyword is encountered during the process of executing the village change, the program will immediately end the current loop and start the next onecycle).
Then, Jack encounters another seahorse, his temper is very short-tempered, if you answer correctly, he just closes the knife and you can never go down (for break andStatement: If the break keyword is encountered during the execution of the loop, the program immediately ends the current loop).Jack carefully avoided the correct answer.Successfully passed.
Next, Jack meets a female seahorse and she tells Jack that you're lucky you passed the grumpy seahorse above, or you won't see me.I'm not embarrassing you, just a simple question, let you go there (.else statement is used with loops: Features: If the loop is executed normally, then the code in the else will be executed.; if the loop is forcibly terminated due to the break keyword, the code in the else will not be executed ;).Jack is overjoyed.
In the end, Jack successfully got 10 gold coins, and when he returned to the shore, he met the female police officer (bool), she checked the container on the boat, and if there was nothing in it, she would not allow Jack to go ashore> (bool(): Summary:
1). Convert to bool() for numeric data: except 0, 0.0 returns False, the rest are all True
2). For container types, as long as there is content (withelement), the result is True, otherwise, it is False
3). For None value conversion, the result is False
Usage scenarios:
Often appear in flow control statements
).Jack shows the gold coins to the policewoman and finally landed successfully!


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved