6.1 user input
In Python, user input needs to use the function input() to obtain user input information. The function input() can make the program pause, wait for the user to input some text, and store it in variables after the user inputs it for convenience.
message=intput("Tell me something,and I will repeat it back to you: ") print(message)
When using the function input(), you should specify clear and easy to understand prompts to accurately indicate what information you want users to provide. A space is usually left at the end of the prompt to separate the prompt from user input, so that users can clearly know where their input begins.
Use int() to get numeric input
When using the function input(), Python interprets user input as a string. At this time, if the user inputs a number, there is likely to be a problem in the use process, because the string printout will not make an error, but it will cause an error when you want to use it as a number.
The function int() converts the string representation of a number to a numeric representation.
age=input("How old are you?") age=int(age)
Modulus operator (%)
The modulus operator (%) is a useful tool for processing numerical information. It divides two numbers and returns the remainder.
6.2 introduction to while loop
The for loop is used to create a code block for each element in the collection, while the while loop runs continuously until the specified conditions are not met.
current_number=1 while current_number<=5: print(current_number) current_number+=1
Let the user choose when to exit
When using the while loop to let the program run continuously when the user wants, we can define an exit value. As long as the user does not enter this value, the program will run again.
prompt="\nTell me something,and I will repeat it back to you:" prompt+="\nEnter 'quit' to end the program." message="" while message !='quit': message=input(prompt) print(message)
Sign use
We can define a variable to judge whether the whole program is active. This variable is called flag. In this way, you only need to check one condition in the while statement -- to detect whether the flag is satisfied.
Use break to exit the loop
To exit the while loop immediately without running the rest of the loop, you can use the break statement to exit the loop`
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 to"+city.title()+"!")
Use continue in a loop
To return to the beginning of the loop and decide whether to continue the loop according to the conditional test results, you can use the continue statement, which is different from the break statement, which directly exits the whole loop.
For example, the following only prints odd cycles:
current_number=0 while current_number<10: current_number +=1 if current_number%2==0: continue print(current_number)
6.3 use of while loop in lists and dictionaries
Move elements between lists
Here is a case. In one list, there are newly registered but unauthenticated website users. After users pass the authentication, they can move them to another list. Here, a while loop is used to move elements.
unconfirmed_users=['alice','brian','candance'] confirmed_users=[] while unconfirmed_users: current_user=unconfirmed_users.pop() print("Verifying user: "+current_user.title()) confirmed_users.append(current_user)
Deletes all list elements that contain a specific value
pets=['dog','cat','dog','goldfish','cat','rabbit','cat'] while 'cat' in pets: pets.remove('cat')
Use user input to populate the dictionary
responses={} polling_active=Ture #sign while polling_active: name=input("\nWhat is your name? ") response=input("Which mountain would you like to climb someday? ") responses[name]=response repeat=input("Would you like to let another person respond?(yes/no)") if repeat=='no' polling_active=False