Python: Conditional judgment

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.