2016-12-11 56 views
-4

我想要做的是從用戶獲取特定輸入,如果輸入與規範不匹配,則輸出錯誤消息幾秒鐘並使其消失並獲取輸入再次。 例如:下面我想從用戶獲取字符串並將其存儲在名爲'name'的變量中。因此,如果用戶提供了長度爲20的字符串,我想要打印'TOO LONG!(MAX:15)',但在此之後,新的提示將從此處開始(因爲循環)。如果用戶保持提供太長的字符串,這會越來越遠。所以我想打印'太長!(MAX:15)'一次,並使其在幾秒鐘後消失,並讓用戶回到上部提示。 這就是我想要做的是:如何使字符串在Python中消失一段時間

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 

完整的腳本:

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() 
+0

您的示例代碼是否可以縮小爲範圍(N)中的n:print('something')'''?這是你的問題 - 如何在n次迭代後在同一行上打印它? – wwii

+0

[動態打印在一行中]可能的重複(http://stackoverflow.com/questions/3249524/print-in-one-line-dynamically) – wwii

+0

[輸出到同一行覆蓋以前的輸出?](http:/ /stackoverflow.com/q/4897359/2823755) – wwii

回答

0

這工作在控制檯中。

>>> for n in range(10): 
...  print('foo' + str(n), end = '\r') 
...  time.sleep(.2) 
... 
>>> 
相關問題