需要與方案援助爲我介紹Python的課程:的Python:需要幫助
編寫一個程序,要求用戶對包含他們的第一個,中間和最後一個名稱的字符串。程序將修改輸入以顯示輸入的首字母。如果一個人輸入「NA」作爲中間名,那麼程序應該只顯示姓和名的首字母縮寫。使用以下字符串測試程序:
Alfred E. Newman A.E.N.約翰·史密斯J.S.
這是我到目前爲止有:
def main():
index = 0
#first_name = input("Please enter your first name: ")
#middle_name = input("Please enter your middle name: ")
#last_name = input("Please enter your last name: ")
#first_initial = first_name[0].upper() + "."
#middle_initial = middle_name[0].upper() + "."
#last_initial = last_name[0].upper() + "."
#print("Here are your initials: ", first_initial, middle_initial, last_initial)
full_name = input("Please enter your full name (with spaces): ")
f_i = ""
m_l_i = ""
for ch in full_name:
if index == 0:
f_i = ch.upper() + "." + " "
if ch == " ":
index += 1
m_l_i += full_name[index].upper() + "." + " "
index += -1
index += 1
full = f_i + m_l_i
print("Your initials are: ", full)
main()
程序工作,但我有添加IF的,如果中間名是「NA」問題
自己做好你的功課。 – mikeb
您可能會發現['split []](https://docs.python.org/2/library/string.html#string.split)輸入字符串更容易。另外,我會避免像'f_i'這樣的變量名。使用更具描述性的內容。 –