#! /usr/bin/python3 # ================================================================== # comand line user interface # ================================================================== import os import platform import sys # ------------------------------------------------------------------- # running Python3 ? # ------------------------------------------------------------------- def RunningPython3(): ##print(sys.version_info) if sys.version_info[0] == 3: return True return False # ------------------------------------------------------------------- # prompt the user for input # ------------------------------------------------------------------- def GetUserInput(prompt,py3): if py3: return input(prompt) else: return raw_input(prompt) # ------------------------------------------------------------------- # pause program # ------------------------------------------------------------------- def Pause(py3): print('') GetUserInput('Press enter to continue ',py3) # ------------------------------------------------------------------- # clear the terminal screen # ------------------------------------------------------------------- def ClearScreen(): if platform.system() == 'Linux': os.system('clear') elif platform.system() == 'Windows': os.system('cls') else: pass # ------------------------------------------------------------------- # main - test UI functions # ------------------------------------------------------------------- if __name__ == "__main__": py3 = RunningPython3() LoopCounter = 0 while True: LoopCounter += 1 ClearScreen() print() value = GetUserInput('[{}] Enter an integer: '.format(LoopCounter),py3) sval = value.strip() if sval == '': break if sval.isdigit() != True: print() print('Illegal value entered({})'.format(sval)) Pause(py3) continue ival = int(sval) print() print("Input type: {} value: {}".format(type(sval),sval)) print("Converted type: {} value: {}".format(type(ival),ival)) Pause(py3) print()