-3

What i want to do is get specific input from the user and if the input does not match with specification, print an error message for few seconds and make it disappear and get the input again. Ex: below i want to get string from user and store it in a variable called 'name'. length of the string must be no more than 15. So if user supplied string of length 20, i want to print 'TOO LONG!(MAX:15)' but after that new prompt start right below that(because of the loop) . If user keep supplying too long strings, this going further and further. so i want to print 'TOO LONG!(MAX:15)' once and make it disappear after few seconds and get user back to the upper prompt . This is where i want to do it:

def get_contact():
    try:
        while True:
            name = input('\033[1m\nNAME: \033[0m')
            if name.isalpha():
                if len(name) > 16:
                    print('\033[91m\033[1mTOO LONG!(MAX:15)\033[0m\033[0m')
                    continue
                break
            else:
                print('\033[91m\033[1mALPHABETICAL CHARACTORS ONLY!\033[0m\
                    \033[0m')
        while True:
            number = input('\033[1m\nNUMBER: \033[0m')
            if number.isnumeric():
                if len(number) > 16:
                    print('\033[91m\033[1mTOO LONG!(MAX:15)\033[0m\033[0m')
                    continue
                break
            else:
                print('\033[91m\033[1mNUMBERS ONLY!\033[0m\033[0m')
    except EOFError:
        print('\033[1m\n\nYOU CANCELED THE OPERATION!\033[0m')
        time.sleep(1)
        main_view()
    except KeyboardInterrupt:
        time.sleep(1)
        os.system('clear')
        exit(0)

    return name, number

Full script:

import pickle, os, shutil, time

window_width, window_height = shutil.get_terminal_size()

def header():
    os.system('clear')

    line = '=' * window_width
    welcome = 'PHONEBOOK'.center(window_width)
    print('\033[1m{0}\n{1}\n{0}\033[0m\n'.format(line, welcome))

def main_view():
    header()
    data = None
    try:
        data = open('phonebook.data', 'rb')
        phonebook = pickle.load(data)
    except FileNotFoundError:
        phonebook = {}

    if phonebook:
        print('\033[1m{0:20s}{1}\033[0m\n'.format('Name', 'Number'))
        for name, number in sorted(phonebook.items()):
            print('{0:20s}{1}'.format(name, number))
    else:
        print('\033[1mTHERE ARE NO ENTRIES.\033[0m\n')

    if data:
        data.close()

    footer('main', phonebook)

def add_new(phonebook):
    header()

    name, number = get_contact()

    phonebook.update({name : number})

    update_file(phonebook)

    footer('addnew')

def delete(phonebook):
    header()
    print('\033[1mENTER THE NAME OF THE CONTACT YOU WANT TO DELETE.\033[0m\n')

    contact = get_input(': ')
    found = isfound(phonebook, contact)

    if found:
        print('\033[1m\nDO YOU REALY WANT TO DELETE:\033[0m {0}({1})'\
            .format(contact, phonebook[contact]))
        print('\033[1m(YES FOR CONFIRM ANYTHING ELSE FOR CANCEL )\033[0m')

        confirm = get_input(': ')
        print(confirm)
        if confirm == 'YES':
            del phonebook[contact]

            update_file(phonebook)

            footer('delete')

        else:
            time.sleep(1)
            main_view()

    else:
        print('\nNOT FOUND')
        time.sleep(1)
        delete(phonebook)

def modify(phonebook):
    header()
    print('\033[1mENTER THE NAME OF THE CONTACT YOU WANT TO MODIFY.\033[0m\n')

    contact = get_input(': ')
    found = isfound(phonebook, contact)

    if found:

        name, number = get_contact()

        del phonebook[contact]
        phonebook.update({name : number})

        update_file(phonebook)

        footer('modify')
    else:
        print('\nNOT FOUND')
        time.sleep(1)
        modify(phonebook)

def footer(view, phonebook=None):
    if view == 'main':
        text = '\033[1mADD NEW(a)   MODIFY(m)   DELETE(d)   SEARCH(s)\
    EXIT(CTRL-D)\033[0m'.center(window_width)
        print('\n',text)

        try:
            action = input(': ')
        except (EOFError, KeyboardInterrupt):
            time.sleep(1)
            os.system('clear')
            exit(0)

        if action == 'a':
            add_new(phonebook)
        elif action == 'm':
            modify(phonebook)
        elif action == 'd':
            delete(phonebook)
        else:
            print('\nINVALID!')
            time.sleep(1)
            main_view()

    elif view == 'addnew' or view == 'modify' or view == 'delete':
        print('\033[1m\nSUCCESSFUL!\033[0m')
        time.sleep(1)
        main_view()

def get_contact():
    try:
        while True:
            name = input('\033[1m\nNAME: \033[0m')
            if name.isalpha():
                if len(name) > 16:
                    print('\033[91m\033[1mTOO LONG!(MAX:15)\033[0m\033[0m')
                    continue
                break
            else:
                print('\033[91m\033[1mALPHABETICAL CHARACTORS ONLY!\033[0m\
                    \033[0m')
        while True:
            number = input('\033[1m\nNUMBER: \033[0m')
            if number.isnumeric():
                if len(number) > 16:
                    print('\033[91m\033[1mTOO LONG!(MAX:15)\033[0m\033[0m')
                    continue
                break
            else:
                print('\033[91m\033[1mNUMBERS ONLY!\033[0m\033[0m')
    except EOFError:
        print('\033[1m\n\nYOU CANCELED THE OPERATION!\033[0m')
        time.sleep(1)
        main_view()
    except KeyboardInterrupt:
        time.sleep(1)
        os.system('clear')
        exit(0)

    return name, number

def get_input(string):
    try:
        var = input(string)
    except EOFError:
        print('\033[1m\n\nYOU CANCELED THE OPERATION!\033[0m')
        time.sleep(1)
        main_view()
    except KeyboardInterrupt:
        time.sleep(1)
        os.system('clear')
        exit(0)

    return var

def isfound(phonebook, contact):
    for name, number in phonebook.items():
        if contact == name:
            return True
    return False

def update_file(phonebook):
    data = open('phonebook.data', 'wb')
    pickle.dump(phonebook, data)
    data.close()

if __name__ == '__main__':
    main_view()
Xcodian Solangi
  • 2,170
  • 4
  • 21
  • 49
Raveen Asela
  • 11
  • 1
  • 4
  • Could your example code be reduced to ```for n in range(N): print('something')```? And is your question - How can I make it print on the same line after n iterations? – wwii Dec 11 '16 at 16:43
  • Possible duplicate of [Print in one line dynamically](http://stackoverflow.com/questions/3249524/print-in-one-line-dynamically) – wwii Dec 11 '16 at 17:18
  • [output to the same line overwriting previous output ?](http://stackoverflow.com/q/4897359/2823755) – wwii Dec 11 '16 at 17:24

1 Answers1

1

This works in the console.

>>> for n in range(10):
...     print('foo' + str(n), end = '\r')
...     time.sleep(.2)
...
>>>
wwii
  • 19,802
  • 6
  • 32
  • 69