Python - Introduction User input

Introduction

In Python, typical code for such an interactive loop might look like this:

while True: 
    reply = input('Enter text:(type stop to stop)') 
    if reply == 'stop': break 
    print(reply.upper()) 

The code leverages the Python while loop.

The input built-in function is used here for general console input.

It prints its optional argument string as a prompt and returns the user's typed reply as a string.

Use raw_input in Python 2.X instead.

A single-line if statement that makes use of the special rule for nested blocks.

The body of the if appears on the header line after the colon instead of being indented on a new line underneath it.

The Python break statement is used to exit the loop immediately.

Related Topics