1, the function input () lets the program pauses the operation, waits for the user to enter some text. Once the user input is obtained, Python stores it in a variable for ease of use.
message = input ("Please input your name here:")
Print (message)
Output:
[Email protected] python-test]#./test_python.py
Please input your name Here:alex
Alex
The function input () receives a parameter: the prompt or description to be displayed to the user, letting the user know what to do.
2, if the input () function needs to prompt or description of the content is too long, you can separate multiple lines of writing, for example:
prompt = "If You have us who is, we can personalize the messages you see."
Prompt + = "\nwhat is your first name?"
Name = input (prompt)
Print (name)
Output:
[Email protected] python-test]#./test_python.py
If you tell us who is, we can personalize the messages you see.
What is your first name? Alex
Alex
3. For the input () function, the inputs are treated as strings, and an int () function is required if an attempt is made to use the input as a number.
#!/usr/bin/env Python3
Age = Input ("What old is You?")
if int (age) >= 18:
Print ("Age is greater than 18.")
elif Int (age) < 18:
Print ("Age is smaller than 18.")
Output:
[Email protected] python-test]#./test_python.py
How is old is you? 21st
Age is greater than 18.
[Email protected] python-test]#./test_python.py
How is old is you? 11
Age is smaller than 18.
4, the modulo operator (%) divides two numbers and returns the remainder:
>>> 5%2
1
>>> 6%3
0
>>> 7%4
3
>>>
The remainder is 0, which means that it can be divisible. The modulo operation can be used to determine whether a number is odd or even. If it is an even number, the number% 2 = = 0, otherwise it is odd.
5. If the python2.x version is used, the function that gets the input is raw_input (), and similarly, the obtained input is also considered a string. The input () function is also available in the python2.x version, but it is not recommended, use the Raw_input () function.
6. For loop is used to have a code block for each element in the collection, while the while loop runs continuously until the specified condition is not satisfied.
Current_number = 1
While Current_number < 5:
Print (Current_nmuber)
Current_number + = 1
7. In a program that requires many conditions to continue running, you can define a variable that determines whether the entire program is active. This variable is called a sign and acts as a traffic light for the program. You can let the program continue running when the flag is true, and let the program stop if any event causes a flag value of false.
prompt = "\ntell me something, and I'll repeat it back to you:"
Prompt + = "\nenter ' quit ' to end of the program."
Active = True
While active:
Message = input (message)
If message = = ' Quit ':
Active = False
Else
Print (message)
8. Use break to exit the loop. To exit the while loop immediately, no longer runs the rest of the code in the loop, or you can use the break statement regardless of the result of the conditional test. The break statement is used to control which lines of code are executed and which line of code is not executed, allowing the program to execute the code that you want to execute as required by the procedure.
prompt = "\nplease enter the name of a city you have visited:"
prompt + = "\ n (Enter ' quit ' when you are finished.)"
While True:
City = input (prompt)
If City = = ' Quit '
Break
Else
Print ("I ' d love to Go" + city.title () + "!")
The break statement can also be used in a for loop.
9, use continue in the cycle. You can use the Continue statement to return to the beginning of the loop and decide whether to continue the loop based on the test results of the condition. It does not execute the rest of the code like a break statement and exits the entire loop.
Current_number = 0
While Current_number < 10:
Current_number + = 1
If Current_number% 2 = = 0:
Continue
Print (Current_number)
This is equal to the odd number of 1~9 printed out, and encountered even if the loop jumps out.
10, remember to avoid infinite cycle.
11, A For loop is an efficient way to iterate through a list, but the list should not be modified in the For loop, which would otherwise make it difficult for Python to track the elements. To modify a list while traversing it, you can use the while loop. By combining the while loop with lists and dictionaries, you can collect, store, and organize large amounts of input for later viewing and display.
12. Move the elements between the lists:
Unconfirmed_users = [' Alex ', ' Johnny ', ' Ruifeng ']
Confirmed_users = []
While Unconfirmed_users:
Current_User = Unconfirmed_users.pop ()
Print ("Verifying User:" + current_user.title ())
Confirmed_users.append (Current_User)
Print ("\nthe following users have been confirmed:")
For Confirmed_user in Confirmed_users:
Print (Confiremd_user.title ())
13. Delete all list elements that contain a specific value. We can use the function remove () to remove a specific value from the list, but only the first occurrence of the value is deleted. If you need to delete more than one value, you can do it with a while loop:
Pets = [' dog ', ' cat ', ' dog ', ' goldfish ', ' rabbit ', ' cat ', ' cat ']
Print (pets)
While ' Cat ' in Pets:
Pets.remove (' Cat ')
Print (pets)
14. Use user input to populate the dictionary.
responses = {}
Poll_activee = True
While poll_active:
Name = input ("\nwhat is your name?")
Response = input ("which mountain would")
Responses[name] = response
repeat = input ("Would you like to let another person respond? (yes/no) ")
if repeat = = ' no ':
Polling_active = False
Print ("\ n---Poll Results---")
For name, response in Responses.items ():
Print (name + "would like to climb" + response + ".")
This article is from the "Alex Blog" blog, make sure to keep this source http://houjun19830610.blog.51cto.com/9269760/1847299
"Python Beginner-7" user input and while loop