python conditional branches - zero-based learning python 007

Foreword

if else conditional statement before the blog also mentioned that there is not winded at Hello. Continued would use examples demonstrated how to use if else conditional branching? What are the benefits of using conditional branch is? How to use conditional expressions (ternary operator) to simplify your code? Understanding assert (assertion) Keyword

table of Contents:

if else conditional branching

If the system 100 according to, 90 or more score points A, 80 to 90 is a B, 60 to 80 is a C, 60 hereinafter is D, write a program when the user inputs the score is automatically converted to ABCD printing form. How to write it? Can be written with conditional branches probably write the following three.

The first:

score = float(input("请输入成绩:"))
if(score < 0 or score > 100):
    print("输入成绩错误!设定范围0到100!")
if(score>=90):
    print("成绩评级为:A")
if(80<=score<90):
    print("成绩评级为:B")
if(60<=score<80):
    print("成绩评级为:C")
if(score<60):
    print("成绩评级为:D")

Can know if the program each statement is independent, front and rear are not necessarily linked, in accordance with the order of execution. If after we entered 101 points, then it is judged wrong score, they will continue to back the implementation of the program to determine whether it is whether A is B, then it will lead to a waste of resources over time.

The second:

score = float(input("请输入成绩:"))
if(score < 0 or score > 100):
    print("输入成绩错误!")
else:
    if(score >= 90):
        print("成绩评级为:A")
    else:
        if(80 <= score < 90):
            print("成绩评级为:B")
        else:
            if(60 <= score < 80):
                print("成绩评级为:C")
            else:
                if(score < 60):
                    print("成绩评级为:D")
      

Third:

score = float(input("请输入成绩:"))
if(score < 0 or score > 100):
    print("输入成绩错误!")
elif(score >= 90):
    print("成绩评级为:A")
elif(80 <= score < 90):
    print("成绩评级为:B")
elif(60 <= score < 80):
    print("成绩评级为:C")
else:
    print("成绩评级为:D")

In fact, the second and third effect is the same, only a third of the wording more concise. elif statement is, in fact, nothing else if abbreviated.

The conditional expression (ternary operator)

And assignment completion condition is determined by an operation statement, num = a if (a < b) else b, which is the conditional expression ternary operator if the latter condition is satisfied if assigned to put a num, or put b assigned to num.
For example, I wrote a comparison of any two numbers the size of the program:

a = float(input("请输入第一个数:"))
b = float(input("请输入第二个数:"))
if(a<b):
    small = str(a)
    big = str(b)
    print("比较小的是:"+ small)
    print("比较大的是:"+ big)
else:
    small = str(b)
    big = str(a)
    print("比较小的是:"+ small)
print("比较大的是:"+ big)

Ternary operator condition improved expression:

a = float(input("请输入第一个数:"))
b = float(input("请输入第二个数:"))
small = str(a if (a<b) else b)
big = str(b if (a<b) else a)
print("比较小的是:" + small)
print("比较大的是:" + big)

About apparently a lot of it!

Keywords: assert (assertion)

Conditions assert keyword behind if false, the program will throw an exception AssertionError, such as
Here Insert Picture Description
Condition 3> 4 is false, so prompt abnormal; Condition 3 <4 is true, there is no error.

Therefore, you can use the keyword assert as a monitoring point of the program. When the need to ensure that the program of a certain condition is true in order for the program to work, when you can assert a.

END

I jammny, like the point of a two Zanga attention to it! Continuously updated series of zero-based learning python.

Published 19 original articles · won praise 52 · views 10000 +

Guess you like

Origin blog.csdn.net/qq_41832837/article/details/104052634