This article is participating in Python Theme Month. See the link for details

Most programs are designed to solve the end user’s problem, and to do so usually requires obtaining some information from the user. To do this, you will use the function input(). How to keep the program running so that users can input information as needed and use it in the program. You can use a while loop to keep the program running until the specified condition is not met.

functioninput()The working principle of

  • functioninput()Let the program pause and wait for the user to enter some text.
  • After taking user input, Python assigns it to a variable that you can use easily.
message = input("Tell me something, and I will repeat it back to you:")
print(message)
Copy the code
  • functioninput()Accept a parameter — a prompt or instruction to display to the user so that the user knows what to do.

Write clear programs

  • Whenever I use a functioninput()You should specify clear, easy-to-understand prompts that specify exactly what information you want the user to provide.
  • By including a space at the end of the prompt (in this case, after a colon), you separate the prompt from the user’s input and let the user know exactly where his input began.
name = input("Please input your name: ")
print(f"Hello, {name}!")
Copy the code
  • Sometimes, the prompt can be longer than one line.
  • In this case, the prompt can be assigned to a variable, which can then be passed to the functioninput(). That way, even if the prompt is more than one line,input()Statements will also be very clear.
prompt = 'If you tell us who you are, we can personalize the messages you see.'

prompt += '\nWhat is your first name? '

name = input(prompt)

print(f"Hello, {name}!")
Copy the code

useint()To get numeric input

  • Using the functioninput()Python interprets user input as a string.
  • functionint()Converts a string representation of a number to a numeric representation.
age = input("How old are you? ")
age = int(age)
print(age > 18)
Copy the code
  • Be sure to convert numeric input to numeric representation before using it for calculation and comparison.

whilecycle

  • forLoops are used to execute a code block for each element in the collection, whilewhileThe loop keeps running until the specified condition is not met.

usewhilecycle

current_number = 1

while current_number <= 5:
    print(current_number)
    current_number += 1
Copy the code

Let the user choose when to quit

  • You can use a while loop to keep the program running as long as the user wants.
prompt = "Tell me something, and I will repeat it back to you:"
prompt += '\nEnter "quit" to end the program. '

message = ""

whilemessage ! ='quit':
    message = input(prompt)

    ifmessage ! ='quit':
        print(message)
Copy the code

Use sign

  • Set the variable active toTrueTo make the program initially active.
prompt = "Tell me something, and I will repeat it back to you:"
prompt += '\nEnter "quit" to end the program. '

active = True

while active:
    message = input(prompt)

    if message == 'quit':
        active = False
    else:
        print(message)
Copy the code

usebreakExit the loop

  • Exit immediatelywhileLoop, which no longer runs the rest of the code in the loop, regardless of the result of the conditional test, can be usedbreakStatements.
prompt = 'Please 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(f"I'd love to go to {city.title()}!")
Copy the code

Use in a loopcontinue

current_number = 0

while current_number < 10:
    current_number += 1
    if current_number % 2= =0:
        continue
    print(current_number)
Copy the code

Avoid infinite loops

  • eachwhileLoops must have a way of stopping so they don’t go on forever.