1. Python User interaction
The program will inevitably interact with the user.
For example, you would want to get the user's input and print out some returned results to the user. We can implement this requirement by using the input () function and the print function, respectively.
1 #! /user/bin/env Ptyhon2 #-*-coding:utf-8-*-3 #Author:visonwong4 5Name = input ("Name:")6age = Int (input ("Age :"))7Job = input ("Job:")8salary = Int (input ("Salary:"))9 Teninfo =""" One --------Info of {0}------- A name:{0} - Age:{1} - job:{2} the Salary:{3} - """. Format (name,age,job,salary) - - Print(info)
Output Result:
1 E:\Python\PythonLearing\venv\Scripts\python.exe e:/python/ Pythonlearing/test.py 2 name:visonwong 3 age:27 4 job:code farmer 5 salary:10000< Span style= "COLOR: #008080" > 6 7 --------info of visonwong------- 8 name:visonwong 9 age:2710 job:code farmer 11 salary:1000012 14 Process finished with exit code 0
It is important to note that because age and salary are numbers, they are forced into shaping.
2. Python Logic control
If statement:
In Python programming, if statements are used in the execution of control programs, in the basic form:
1 if Judging Condition: 2 EXECUTE statement ... 3 Else : 4 Execute statement ...
Where the "judging condition" is established (not 0), the following statements are executed, and the execution content can be multiple lines, in order to be indented to differentiate the same range.
Else as an optional statement, you can execute the relevant statement when you need to execute the content when the condition is not true, as shown in the following example:
1 #! /user/bin/env Ptyhon2 #-*-coding:utf-8-*-3 #Author:visonwong4 5 6Name = input ('Please enter user name:')7PWD = input ('Please enter your password:')89ifName = ="Visonwong" andPWD = ="cmd":Ten Print("Welcome, vison!. ") One Else: A Print("User name and password error!")
Output Result:
1 E:\Python\PythonLearing\venv\Scripts\python.exe e:/python/pythonlearing/test.py2 Please enter user name: Visonwong 3 Please enter password: cmd 4 Welcome, vison!. 56 Process finished with exit code 0
When judging a condition as multiple values, you can use the following form:
1 if Judging condition 1: 2 EXECUTE statement 1 ... 3 elif Judging condition 2: 4 EXECUTE statement 2 ... 5 elif Judging condition 3: 6 EXECUTE statement 3 ... 7 Else : 8 Execute Statement 4 ...
Examples are as follows:
1 #! /user/bin/env Ptyhon2 #-*-coding:utf-8-*-3 #Author:visonwong4 5Age = 276 7user_input = Int (input ("input your guess num:"))8 9 ifUser_input = =Age :Ten Print("Congratulations, you got it!") One elifUser_input <Age : A Print("Oops,think bigger!") - Else: - Print("Oops,think smaller!")
Output Result:
1 E:\Python\PythonLearing\venv\Scripts\python.exe e:/python/pythonlearing/test.py2 input your guess num:253oops,think bigger! 4 5 Process finished with exit code 0
Python Learning path 3--python user interaction and logic control