Python--if, elif, Else statements

Source: Internet
Author: User

First, the process branch

1. Single branch:

Example 1: Establish a judgment, enter age, determine whether the age is greater than 23, if greater than 23, print the result: ' It is time to find a bf ':

1 age_of _you=int (Input ('please input your name:'))2 age_of _ you=253if age_of _you >:4     print('it is Time to find a BF)

2. Dual branch:

Example 2: Enter a user name and password to welcome the user when the user name and password are correct, otherwise prompt error

1_username='Shanshan'2_password='123456'3 4Username=input ('Pleaue Input your name:')5Password=input ('Pleaue Input your password:')6 7 if_username==username and_password==Password:8     Print('Welcome', username)9 Else:Ten     Print('worong username or password')

3. Continue branching under branches

Example 3: Input age, gender, name, if it is a girl, age <28, Output ' I love girls ', age >28, output siblings is also very good oh, if it is boys, output together to make a base bar

1Name=input ('Name:')2Sex=input ('Sex:')3Age=int (Input ("Age :"))4 5 ifsex=='F':6     ifAge<28:7         Print('I Love Girls')8     Else:9         Print('brother and sister Love is also very good oh')Ten Else: One     Print('let's do it together.')

4. Multi-Branch

Example 4: a:90~100,b:80~89,c:60~79,d:40~59,e:<39 to Grade,

1Grade=int (Input ('Grade:'))2 3 ifGrade>=90:4     Print('A')5 elifGrade>=80:6     Print('B')7 elifGrade>=60:8     Print('C')9 elifGrade>=40:Ten     Print('D') One Else: A     Print('E')

Python--if, elif, Else statements

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.