Condition Control: If...elif...else
if condition_1: statement_1elif condition_2: statement_2Else: Statement_3
Judgment process:
If"condition_1"True to execute the"statement_1"block statement? If"condition_1"is false and will be judged"condition_2"if"condition_2"True to execute the"statement_2"block statement? If"condition_2"To false, the execution of the"Statement_3"Block statements
Example 1:
num = Int (input ("Enter a number:"))ifNum% 2 = =0:ifNum% 3 = =0:Print('The number you enter can be divisible by 2 and divisible by 3.') Else: Print('the number entered can only be divisible by 2, not divisible by 3')Else: ifNum% 3 = =0:Print('the number entered can only be divisible by 3, not divisible by 2') Else: Print('the number entered cannot be divisible by 3 or divisible by 2.')
Output Result:
Please input a number:9 This can only be divisible by 3, not divisible by 2
Example 2: Guess lucky Numbers game
Lucky_num = 7Input_num=-1Guess_num=0 whileLucky_num! = Input_num andGuess_num < 3: Print('Number :', Guess_num) input_num= Int (Input ('input a number:')) ifInput_num >Lucky_num:Print("it ' s too big!!!") elifInput_num <Lucky_num:Print("it ' s too small!!!") Guess_num+ = 1ifLucky_num = =Input_num:Print('Binggo')Else: Print('too many retrs!!!')
Output Result:
Input a number:6it's too small!!! Number:1Input A number:9it's too big!!! Number:2Input A number:8it's too big!!! Number:3Too many retrs!!!
# Play once more! Input A number:8it's too big!!! Number:1Input A number:72Binggo
Python: Conditional judgment