User input and while loop

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: ")

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?")

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.

while current_number<=5:

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."
while message !='quit':

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:
	if city=='quit'
		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:

while current_number<10:
	current_number +=1
	if current_number%2==0:

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.


while unconfirmed_users:
	print("Verifying user: "+current_user.title())

Deletes all list elements that contain a specific value

while 'cat' in pets:

Use user input to populate the dictionary

polling_active=Ture		#sign
while polling_active:
	name=input("\nWhat is your name? ")
	response=input("Which mountain would you like to climb someday? ")
	repeat=input("Would you like to let another person respond?(yes/no)")
	if repeat=='no'

Keywords: Python

Added by gl_itch on Tue, 28 Dec 2021 04:15:15 +0200