标签:python
输入格式:Last Name, First name
如果输入的格式: First name LastName(没有逗号)
互换位置,提示错误,记录错误数
输入完毕后,排序输出
这里以q退出,done输入完毕
all = [] count = 0 error = 1 while True: name = input("Please enter name %d :" % count) if len(name.split(‘,‘)) == 2: all.append(name) count += 1 elif name == ‘q‘: break elif name == ‘done‘: print("The sorted list (by last name) is:") for i in sorted(all): print(‘ ‘,i) break elif len(name.split(‘ ‘)) == 2: tmp = name.split(‘ ‘) name = tmp[1].strip() + ‘, ‘ + tmp[0].strip() all.append(name) print("Wrong format... should be Last, First.") print("You have done this %d time(s) already.Fixing input..." % error) error += 1 count += 1 else: print("enter error, try agent.") continue
Please enter name 0 :Smith, Joe
Please enter name 1 :Mary Wong
Wrong format... should be Last, First.
You have done this 1 time(s) already.Fixing input...
Please enter name 2 :Hamilton, Gerald
Please enter name 3 :done
The sorted list (by last name) is:
Hamilton, Gerald
Smith, Joe
Wong, Mary
本文出自 “Halo” 博客,请务必保留此出处http://ihalo.blog.51cto.com/7185748/1790480
姓名格式处理,"Last Name, First Name"
标签:python
原文地址:http://ihalo.blog.51cto.com/7185748/1790480