function input() Pause a program , Wait for the user to enter some text , It can be used as a prompt to enter relevant content .
for example :
>>>massage = input("Tell me something,and i will repeat it back to you:")
Tell me something,and i will repeat it back to you:hello everyone
>>>print(massage)
hello everyone
notes :hello everyone Enter the content for the program after it runs .
therefore , Using functions input() You can give a prompt for the input content before the user input .
function int() Converts a string representation of a number to a numeric representation .
for example :
>>>age = input("how old are you:")
Operation output :how old are you: 20
>>>age >= 18
Application error , This is because the value we entered ‘20‘ It is represented by a string , If we use it as a number , Will cause an error . here , We need to use int() To get numerical input , as follows :
>>>age = int(age)
>>>age >= 18
Operation output :True
The program runs successfully , therefore : Use numerical input before calculation and comparison , Be sure to convert it to a numerical representation .
for Loops are used for a block of code for each element in the collection , and while The cycle goes on and on , Until the specified conditions are not met .
Use while loop , below , Let's do a simple loop :
number = 1
while number <= 5:
print(number)
number += 1
This is a cycle of counting ,number Set to 1, Specify number from first , Next while The loop is set like this : as long as number Is less than or equal to 5, And then run this cycle , Code printing in the loop number Value , Each cycle number Add one more .
1
2
3
4
5
while The loop lets the user choose the appropriate exit , We can define an exit value in the recycle , As long as the user does not enter this value , The program goes on to run .
promot = "\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)
if message != 'quit':
print(message)
At the beginning of the program , We define a message , Tell the user two messages , Or enter a message , Either enter the exit value ‘quit. Next , We create a variable message, Used to store values entered by users . We will message The initial value is set to an empty string ""( Give Way Python First execution while There is something on the code line to check ,python First execution while When the sentence is , Need to put message The value of is equal to 'quit' Compare , If the user doesn't type , Nothing to Comparative things , The program will not run , So we have to give message Define an initial value ). We added a to the code if test , If the input is not 'quit' Just print . Let's run this program :
Tell me something,and I will repeat it back to you:
Enter 'quit' to end the program. Hello everyone!
Hello eneryone!
Tell me something,and I will repeat it back to you:
Enter 'quit' to end the program. Hello again.
Hello again.
Tell me something,and I will repeat it back to you:
Enter 'quit' to end the program.quit
We can see that , When we enter other information , The program will run all the time , Until we enter 'quit' when , The program exits and stops running .