You guys may have played the game of Guess the Number It is to randomly offer a number for you to guess After you guess, you will be clued to have guessed a bigger or smaller number In this game, we see a conditional judgment, that is to judge the relationship between the random number and the number you have guessed To solve these problems we need to use the conditional structure we're going to talk about today Any complex program can be solved through three control structures sequence, selection, and repetition We once asked students about this question and some students answered: narration, flashback, and narration interspersed with flashbacks Can you understand the teachers' feelings of hearing those answers? You might be able to In Python, the selection structure is also expressed with "if" statements Let's look at the syntax of "if" statement It is expressed as "if", followed by a conditional expression with a colon A conditional expression consists of comparison operators, member operators or logical operators as well as some operands For instance, we mentioned such a form before "a>b" or with additional "and a < b", etc followed by a code segment When is the code segment executed? It is executed when the condition of expression is True A code segment must be indented usually indented for 4 spaces Let's look at an example: variables "sd1" and "sd2" If they are equal we will output the area of this square Here, the judgment statement is "if sd1 == sd2" It is a conditional expression followed by a colon When the condition is True we execute such a statement It is the usage of "if" statement But, you know in many cases, "if" alone does not suffice to express our question Sometimes we need to execute one thing when the condition is True and, when the condition is False we need to execute another statement It requires the "else" statement then The "else" statement when used, is to be aligned with "if" in syntax followed by a colon as well The code segment after "else" is the statement to be executed when the expression is false The code segment after "else" should also be indented For instance, let's look at an example Input the lengths of two sides and assign them to variables "sd1" and "sd2" Judge whether "sd1" and "sd2" are equal If equal, the program will compute and print "what is the square's area" otherwise, it will print "what is the rectangle's area" If 4 and 4 are input respectively the result will be the area of the square This question is a two-way divergence Let's take test scores as an example It is to divide test scores into two cases those greater than or equal to 60 shall pass and those less than 60 shall fail They are two cases Sometimes, if we need more precise division for example, those greater than or equal to 90 being excellent (A) those between 80 (inclusive) and 90 (not inclusive) being B and those between 70 and 80 being C, and so on Then we need to use "elif" statement Let's look at its syntax rule. It's like this The keywords used here are "if", "elif" to name a few In execution, it's like this If the condition is satisfied, execute this statement If the first condition is not satisfied but the second condition is satisfied execute the following statement Similarly it will go on to the "else" statement "else" means that none of the above conditions is satisfied Then, this statement will be executed For instance, as in the example of test score just now if the score is greater than or equal to 90 execute a statement for printing a tag As for the "elif" below how can we express that it is between 80 and 90? You may write it like ">= 80" How do we understand this expression It means that it does not satisfy the condition of being greater than or equal to 90 but satisfy the condition of being greater than or equal to 80 Does that mean being less than 90 and greater than or equal to 80? So, its execution is like this Well, I assign this instance of score grades to you to finish during the quiz Let's look at a very simple instance next For one thing, we can look at its syntax for another, we may observe its mode of alignment Let's guess what the function of this example is Input the index of shape, and then output the shape For example, for "1", it is a circle Other shapes are also available Here, we see that "if" and "elif" are aligned as well as "else" The code segments below both "if" and "elif" are indented In the final "else" if you do not input the above "1", "2", "3" or "4" the output will be such a result Let's look at the execution result of program If we input "3" it will output this If we input "8" which does not meet any of the above conditions the final result is the last line of string This example is quite simple and its conditions all use the equals sign So, if we change "elif" into "if" there would be no big problem However, logically we should use the mode of "elif" for representation which is designed to solve the multiple-branching structure In the "elif" statement we use what if further branches are needed in one branch For instance, when K equals 3 or 4 and we want to divide that into several cases what is needed here? Yes, it's conditional nesting Let's look at this program Which part adopts conditional nesting? You must have found it Is that when K equals 3 Here, we will differentiate two cases If the two inputs of "sd1" and "sd2" are the same the output is the area of the square like we mentioned before Otherwise, the output would be the area of the rectangle Should this instance be under one condition? And we further judge some different cases Here, we see that we use the "if" and the "else" statement we said before One condition nesting inside another condition constitutes conditional nesting Look at the result of execution If we input "3" we see that those code segments are executed, right? First, you're prompted to input the length of the first side and the length of the second side Then, based on the equality or non-equality relationship of both sides different results will be output We see that "3" and "4" are not equal so the output is the area of the square: 12 If the input is "2" i.e. this branch is executed we see that the output result will be "oval" This is conditional nesting When you need, you may use this mode Then, let's look at the game of Guess the Number mentioned before It is like this the program randomly generates an integer within a range the player makes a guess and the system gives clues Let’s look at this program First, it uses the function "randint()" in the module "random" The effect of this function is to output a number within the range you specify in the argument This is the generated result Then, "x" acquires a number generated randomly within this range This is the "x" value given by the system Next, let's make guesses For guesses , we may use the function "input()" It is a built-in function which can be used to acquire the user's input Judgments are made next If the guess is right print "Bingo!" If it is greater, we may output such a clue "too big" otherwise, it would be "too small" Let's demonstrate it in a program Let's run this program x is a number randomly generated by the system Let's make a guess If we guess it is 31 The program clues "too big" Let's view the value of x It turns to be 11 If we wanna guess again, we may need to run this program again as we only use conditional structure here no loop of random number can be generated For another try we have to execute the program again How can the guess be repeated It needs the loop structure we'll talk about in next section Here, although this instance is not long it uses "if", "elif" and "else" well embodying the thought of conditional structure Well, can this be realized with the concept of conditional nesting Think about it. We surely can We may divided the results of number guessing into two cases guessing right, and guessing wrong For guessing wrong, it can be further divided into having guessed bigger or smaller which would be natural to realize with conditional nesting Let's rewrite it This would be OK By the way as to the conditional expression in Python note that it is a special conditional expression not an expression expressing conditions its form is "x if E else y" During execution, the value of expression expressing a condition is first calculated If the condition is True, returning x otherwise it returns y The most frequently used way of conditional expression is to assign it to a variable say, x = 5, t = 1 if x >= 0 else 0 When x is greater than or equals 0, the operational result of expression is 1 otherwise, it would be 0 In this example, as x is 5 the final value of t is 1 This writing is more concise than our writing like this This is the selection structure in Python I believe you guys have known it fairly well now