Catalog
1. function input() How it works
2.while Introduction to cycle
function input() Pause the program .
Python3.x in input() Function accepts a standard input data , Return to string type .
Python2.x in input() Equivalent to raw_input() , To get input from the console .
example :
1.1 Write a clear program
If the prompt is long / More than one line , The prompt can be stored in a variable , Then pass the variable to the function input()
example :
1.2 Converts the string representation of a number to a numeric value , use int()
Python Interpret user input as characters , If you want to compare characters . You need to use functions int() Convert the string representation of a number to a numeric representation , If it doesn't, it will report an error , The following are examples of error reporting and normal conditions :
1.3 Modulo operators %:
You can divide two numbers and return the remainder , This can be used to judge whether a number is odd or even , example :
2.1 Use while loop , Until the condition ends, the cycle ends
2.2 The user chooses to exit
2.3 Use logo
If in one while Statement to determine multiple conditions , It is very complicated and difficult , In a program that requires many conditions to be met to continue running , You can define a variable , It is used to judge whether the whole program is active or not . This variable is called sign . The program can be marked as True Continue to run , And in any event causes the value of the flag to be False Let the program stop running .
2.4 Use break Exit loop
To quit immediately while loop , No longer run the rest of the code in the loop , You can use break sentence . In any Python Can be used in loops break sentence . for example , You can use break Statement to exit from traversing a list or dictionary for loop .
2.5 Use... In a loop continue
To return to the beginning of the loop , And according to the result of condition test, decide whether to continue to execute the loop , You can use continue sentence , It is not like break Statement to stop executing the rest of the code and exit the loop .
2.6 Avoid infinite loops : Every while Every cycle has to have a way to stop
3. Use while Loop to process the list :
3.1 Move elements between lists
3.2 Deletes all list elements containing the specified value from the list , have access to remove() function To delete a specific value in the list
3.4 Use user input to fill the dictionary , You can use while The loop prompts the user to enter any amount of information .
The learning content of this article comes from 《Python Programming : From introduction to practice 》