This is the 18th day of my participation in Gwen Challenge

Focus on the road ahead so that you don’t stumble. So that you don’t get sidetrack. Give yourself a chance to start over.

Introduction to Python strings and the Time library

Last Time we learned how to use the Time library, here we share an introduction to the wave branching structure and simple operators (including exception handling).

Introduction to single branch structure

Single branch: A running mode in which different forward paths are selected based on the result of the judgment condition.

The code framework is as follows:

Guess =eval (input()) if guess==99: print(" guess right ")Copy the code

Two-branch structure: the operation mode of selecting different forward paths according to the results of judgment conditions.

The code framework is as follows:

If < condition > : < statement block 1> else: < statement block 2> Guess =eval (input()) print(" guess {} ".format(" yes "if guess==99 else" wrong "))Copy the code

To: Compact form only supports expressions, not assignment statements (with = sign)

Multi-branching: A branching structure that selects different statements to run based on multiple criteria, usually using the reserved if elif else word.

Example code is as follows:

Grade ="A" elif score>=80: grade="B" elif score>=70 Grade ="C" elif score>=60: grade="D" print(" format(grade))Copy the code

Operation Description

The three reserved words of the conditional combination are :and or not

Operators and use:

Description of x and y: Logical and of two conditions x and yCopy the code

Operators and use:

X or Y description: Logical or of two conditions x and yCopy the code

Operators and use:

Not X Description: Logical non of condition XCopy the code

Example code:

If guess >99 or guess <99: # example: print(" guess wrong ") else: print(" guess right ")Copy the code

Exception handling

Exception handling: When our program input is illegal or unknown errors occur, we often add related code as a supplement.

For example:

Num =eval(input(" integer ")) print(num**2)Copy the code

To: asks us To enter an integer. When we enter a non-integer or another character such as ABC, the program will report an error at runtime.

We then improved this code:

Try: num=eval(input(" input integer ")) print(num**2) except: print(" invalid input ")Copy the code

This tells us that we usually introduce the following code framework when handling exceptions:

Try: < block 1, no exception > except< exception type >Copy the code

Finally, there is an advanced use of exception handling (code framework) :

Try: < statement block 1, no exception execution > except< exception type >: < statement block 2, exception execution > else: < statement block 3> # Finally: < statement block 4> # Execute when no exception occursCopy the code

The final advanced usage will rarely be used at ordinary times, we can understand, hey hey, today is introduced here first!

About the string and the introduction of the Time library xiaobian here, later if encountered with the knowledge of the relevant, xiaobian will add oh. If this article has been helpful to you, you may be reviewing Python for an exam. I hope you can continue to support this article.

(Python series) To continue…