This article has participated in the activity of “New person creation Ceremony”, and started the road of digging gold creation together
Branching structure
The running mode of different forward paths is selected according to the result of judgment condition
Single branched structure
if> < conditions: true < block > -- > > < conditions - > [block] - > false | ^ | __________________ |Copy the code
# Single branch structure example
guess = eval(input())
if guess == 99:
print("Got it right.")
if Ture
print("The conditions are right.")
Copy the code
Binary structure
if> < conditions: | -- -- -- -- -- > [block2] - | < block1> false| |
elseConditions: - > < > | - > < block2> true | | | -- -- -- -- -- > [block1] - |Copy the code
# Example of two-branch structure
guess = eval(input())
if guess == 99:
print("Got it right.")
else:
print("Guess wrong.")
if Ture:
print ("Block 1")
else:
print("Statement block 2")
Copy the code
Compact form
The two-branch structure is suitable for simple expression
The < expression1> ifConditions of < >elseThe < expression2>
Copy the code
guess = eval(input())
print("Guess {}".format("To" if guess==99 else "Wrong"))
Copy the code
Note: The output of an if else in compact form is not a statement, but an expression, which is part of the statement.
Multibranched structure
A branching structure in which multiple blocks of statements are selected based on conditions
if> < conditions: | -- -- -- -- -- -- -- -- -- - | -- - -- -- -- -- -- - | < block1> [Statement block1[statement block n-1] |
elif: | false | | < block2> -- -- -- > < conditions1> -- >... N - - > < conditions1> -- >[block N] -- >........ tureelse: < statement block2>
Copy the code
# Grading problems for different scores
score = eval(input())
if score>=85:
print("A")
elif score>=70:
print("B")
elif score>=60:
print("C")
else:
print("D")
# Another way of writing (foemat)
score = eval(input())
if score>=90:
grade="A"
elif score>=80:
grade="B"
elif score>=70:
grade="C"
elif score>=60:
grade="D"
print("Input score belongs to level ()".format())
Copy the code
- Note the inclusion relationships between multiple conditions
- Note the coverage of variable values
Conditional judgment and combination
The operator
The operator | Mathematical symbols | describe |
---|---|---|
< | < | Less than |
< = | Or less | Less than or equal to |
> = | p | Greater than or equal to |
> | > | Is greater than |
= = | = | Is equal to the |
! = | indicates | Is not equal to |
Three reserved words
Three reserved words for conditional combinations
Operator and usage | describe |
---|---|
x and y | The logic of two conditions x and y |
x or y | Logical or of two conditions x and y |
not x | Logical non of the condition x |
# sample
guess = eval(input())
if guess > 99 or guess < 99:
print("Guess wrong.")
else :
print("Got it right.")
# sample
if not Ture:
print("Statement block 2")
else:
print("Block 1")
Copy the code
Exception handling of the program
# sample
num = eval(input(Please enter an integer:))
print(num**2)
Copy the code
# exception handling
Tracebak (most recent call last):
File "t.py",line 1.in <module> #line 1 indicates the number of lines in which the exception code occurred
num = eval(input(Please enter an integer:))
File "<string>", line 1.in <module>
NameError: name 'abc' is not defined # Exception type and exception content hint
Copy the code
Two mechanisms for python exception handling
- Use the reserved try word
- Use the reserved word except
Basic use of exception handlingtry: < statement block1>
except: < statement block2> Further distinguish exception typestry: < statement block1>
except< exception type >: < statement block2>
Copy the code
The sample atry :
num = eval(input(Please enter an integer:))
print(num**2)
expect :
print("Input is not an integer") sampletry :
num = eval(input(Please enter an integer:))
print(num**2)
expect NameError:
print("Input is not an integer")
Copy the code
- After an exception type is annotated, only the exception is responded to
- Exception type names are equivalent to variables
Advanced use of exception handling
We can boot blocks 3 and 4 with else and finally after try except
try: < statement block1>
except: < statement block2>
else: < statement block3>
finally: < statement block4>
Copy the code
- Finally block 4 must be executed
- Else block 3 is executed when no exception occurs