我想要做的是从用户那里获取特定输入,如果输入与规格不匹配,则打印错误消息几秒钟并使其消失并再次获得输入。 例如:下面我想从用户那里获取字符串并将其存储在名为“name”的变量中。字符串的长度必须不超过15.因此,如果用户提供长度为20的字符串,我想打印'TOO LONG!(MAX:15)',但在此之后的新提示开始(因为循环)。如果用户继续提供太长的字符串,则会越来越多。所以我想打印'TOO LONG!(MAX:15)'一次,让它在几秒钟后消失,让用户回到上方提示。 这是我想要做的地方:
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 :(得分:0)
这适用于控制台。
>>> for n in range(10):
... print('foo' + str(n), end = '\r')
... time.sleep(.2)
...
>>>