Conditional Judgment of Python basics

Source: Internet
Author: User
Tags ming

Condition to judge a computer can do a lot of automated tasks, because it can make their own conditions to judge.   For example, enter a user's age, print different content according to age, in a Python program, with an IF statement:  age = 20if ages >= 18:    print (' Your is ', Age)     print (' adult ') according to the Python indentation rules, if the IF statement evaluates to True, the indented two lines of the print statement are executed, otherwise, nothing is done.   can also add an else statement to if, meaning if the If judgment is false, do not execute the IF content, go to the else execution:  age = 3if Age >= 18:    Print (' Your age was ', age '     print (' adult ') else:    print (' Your ", age)     Print (' teenager ') note do not write down the colon:.   Of course the above judgment is very sketchy, can be used elif to do more detailed judgment:  age = 3if Age >= 18:    print (' adult ') Elif age >= 6:     print (' teenager ') else:    print (' Kid ') Elif is the else if abbreviation, can have more than one elif, So the complete form of the IF statement is:  if < condition judgment 1>:    < execute 1>elif < condition 2>:    < Execute 2 >elif < condition judgment 3>:    < Execute 3>else:    < Execute 4&GT;IF statement execution has a feature that is judged from the top down, If it is true on a certain judgment, after executing the statement, the remaining elif and else are ignored, so please test and explain why the following processOrder Print is Teenager: age = 20if age >= 6:    print (' teenager ') Elif age >= 18:    Print (' adult ') else:    print (' Kid ') if judging conditions can also be abbreviated, such as write:  if x:    print (' True ') As long as X is a non-0 value, a nonempty string, a non-empty list, and so on, it evaluates to true, otherwise false.   again input finally see a problematic condition to judge. Many students will use input () to read the user input, so that they can input, the program runs more interesting:  birth = input (' Birth: ') if birth < 2000:    print (' 00 ago ') else:    print (' 00 after ') input 1982, resulting in an error:  traceback (most recent call last):  File "<stdin > ", Line 1, in <module>typeerror:unorderable types:str () > int () This is because the data type returned by input () is STR,STR cannot be compared directly to an integer, You must first convert str to integers. Python provides the int () function to do the thing:  s = input (' Birth: ') birth = Int (s) if birth < 2000:    print (' 00 ago ') Else:     print (' 00 after ') runs again to get the right results. But what if I enter ABC? You get an error message:  traceback (most recentcall last):  File "<stdin>", line 1, in <module>valueerror: Invalid literal for int. () with base: ' abc ' original int () function sentNow a string is not a valid number when the error occurs, the program exits.   How do I check and capture the program run-time errors? The subsequent errors and debugs will be covered.   Exercise Xiao Ming is 1.75 tall and weighs 80.5kg. According to the BMI formula (weight divided by the square of height) help Xiao Ming calculate his BMI and:  according to the BMI Index
    • Less than 18.5: too light
    • 18.5-25: Normal
    • 25-28: Overweight
    • 28-32: Obesity
    • Above 32: Severe obesity
Use If-elif to determine and print the result: #-*-coding:utf-8-*-from __future__ Import Division A = input (' Please enter your height: ') b = input ( ' Please enter your weight: ') BMI = float (b/(a*a)) if BMI < 18.5:print (' Your Weight too light ') elif 18.5 <= Bmi&lt ; 25:print (' Your weight is normal ') elif <= BMI <= 28:print (' Your weight too heavy ') Elif < BMI <= 32:print (' Your weight hyperadiposity ') elif BMI > 32:print (' Your weight too hyperadiposity ') else:print (' You R Enter is wrong ') summary condition judgment allows the computer to make its own choice, Python's if...elif...else is very flexible.

Conditional Judgment of Python basics

Related Article

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.