CodyKochmann
9/26/2016 - 5:31 PM

A universal user prompt for python 2 and 3

A universal user prompt for python 2 and 3

from sys import version_info

def prompt_user(prompt="enter text: ", exit_if_empty=False):
    # universal py2 and py3 user prompt
    # by: codykochmann
    out=''
    is_py3 = lambda: version_info >= (3, 0)
    try: out = input(prompt) if is_py3() else raw_input(prompt)
    except KeyboardInterrupt: exit("\nKeyboardInterrupt - exiting") # ctrl+c
    if exit_if_empty and len(out)<1: exit("error: empty value") # empty string
    return out