function input () works:
Sometimes, the hint may be more than one line, for example, you might want to indicate the reason for getting a specific input. In this case,
You can store the hint in a variable and then pass the variable to function input (). This way, even if you are prompted for more than one line, input ()
Statement is also very clear.
" If You tell us who is, we can personalize the messages you see. " "" = input (prompt)print (" " " ! ")
This example shows a way to create a multiline string. The first half of the message is stored in the variable prompt in the 1th row;
In line 2nd, the operator + = Appends a string to the end of the string stored in prompt .
The final hint spans two lines and contains a space after the question mark, which is also a clear consideration:
is your first name? Erichello, eric!
Second, use INT () to get the numeric input:
To solve this problem, you can use the function int (), which allows Python to treat the input as a numeric value. function int () converts the number of characters
The string representation is converted to a numeric representation, as follows:
>>> age = Input ("","" >>> age = Int (age)> >> Age >=True
How do I use the function int () in the actual program? Take a look at the following procedure, which determines whether a person satisfies a roller coaster ride.
Height Requirements:
Height = input ("How tall is you, in inches?") Height=int (height)ifHeight >= 36:Print("\nyou ' re tall enough to ride!")Else:Print("\nyou ' ll is able to ride when you ' re a little older.") How tall is you,inchInches? 71 You're tall enough to ride!
Attention:
If you are using Python 2.7, you should use the function raw_input () to prompt the user for input. This function with Python 3
Input () is also interpreted as a string.
Three, while loop introduction:
The For loop is used for a block of code for each element in the collection, while the while loop continues to run until the specified bar
The item is not satisfied.
Current_number = 1 while Current_number <= 5:print+ = 1
Four, let the user choose when to quit
You can use the while loop to keep the program running as the user wishes, as shown in the following program parrot.py. in which we
An exit value is defined, and the program runs as long as the user does not enter this value:
prompt ="\ntell me something, and I'll repeat it back to you:"prompt+="\nenter ' quit ' to the end of the program."message="" whileMessage! ='quit': Message=input (Prompt)ifMessage! ='quit':Print(message) Tell me something, andI'll repeat it back to You:enter'quit'To end of the program . Hello everyone! Hello everyone! Tell me Something, andI'll repeat it back to You:enter'quit'To end of the program . Hello again. Hello again. Tell me Something, andI'll repeat it back to You:enter'quit'To end of the program. Quit
Five, the use of signs
Next, add a flag in the program parrot.py in the previous section. We name this flag active (it can be referred to as
It will be used to determine if the program should continue to run:
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 (prompt)? if message = = " quit " :active = False? else : print (message)
In the while loop, we use an if statement after the user has entered to check the value of the variable message. If the user enters
Is ' Quit ' (see?), we set the variable active to false, which causes the while loop to no longer execute. If
The user does not enter ' quit ' (see?), we print the input as a message.
Vi. using break to exit the loop
To exit the while loop immediately, no longer runs the rest of the code in the loop, and regardless of the result of the condition test, you can use the
Break statement. The break statement is used to control which lines of code are executed and which line of code is not
So that the program executes the code you want to execute according to your requirements.
" \nplease enter the name of a city that has visited: " "while = input (prompt)if ' quit ' : Break
Else
Print ("I ' d love to Go" + city.title () + "!")
Please enter the name of a city that has visited:
(Enter ' Quit ' when you are finished.) New York
I ' d love to go to New york!
Please enter the name of a city that has visited:
(Enter ' Quit ' when you are finished.) San Francisco
I ' d love to go to San francisco!
Please enter the name of a city that has visited:
(Enter ' Quit ' when you are finished.) Quit
Ix. use of continue in circulation
To return to the beginning of the loop and decide whether to continue the loop based on the test result of the condition, use the Continue statement, which
Unlike the break statement, the rest of the code is no longer executed and the entire loop exits. For example, look at a number from 1 to 10, but only print
One of the even loops:
Current_number = 0 while Current_number <: + = 1 if current_number% 2 = = 0: continue print(current_number)13579
Ten, avoid infinite circulation;
Each while loop must have a way to stop running, so that it does not go on indefinitely. For example, the following
Loops from 1 to 5:
x = 1 while x <= 5:print+ = 1
But if you accidentally omit the code line x + = 1 as follows, this loop will run endlessly:
# This cycle will run endlessly! x = 1 while x <= 5:print(x)
Python Learning 6