氏名フォーマット処理、「Last Name,First Name」
1318 ワード
入力形式:Last Name,First name入力形式:First name LastName(カンマなし)交換位置、エラーメッセージ、レコードエラー数入力完了後、ソート出力
ここでqで終了し、done入力完了
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
ここで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