以下のPythonコードを実行すると、3つの異なるシナリオに基づいてユーザー名を作成しようとしています(1.userがFirst、 (姓)、2.ユーザーが姓と名字を入力する3.ユーザーが姓のみを入力する姓の最初の4文字に基づいてユーザー名を作成しようとしています。 )。私は最後の+最初の名の組み合わせまたはちょうど最後の名前を使用して、以下のコードを実行すると、私は別々の出力を作成するために異なる基準に基づいて異なる分割を実行します
lastName = (listnames[2]) IndexError: list index out of range
def entirename(fullName):
lowername = str.lower(fullName)
delimiter =' '
listnames = (lowername.split(delimiter))
lastName = (listnames[2])
lname = lastName[0:4]
fInit = listnames[0]
fname = fInit[0]
mInit = listnames [1]
mname = mInit[0]
username =lname+fname+mname
return
def firstlast(fullName):
lowername = str.lower(fullName)
delimiter =' '
listnames = (lowername.split(delimiter))
lstName = (listnames[1])
lname = lastName[0:4]
fInit = listnames[0]
fname = fInit[0]
username = lname+fname
return
def last(fullName):
lowername = str.lower(fullName)
delimiter =' '
listnames = (lowername.split(delimiter))
lstName2 = (listnames[0])
lname = lstName2[0:4]
username = lname
return
def main():
print "Hello! This program will contruct a new userid for you."
print ""
fullName = raw_input('Please enter your full name: ')
while True:
if fullName[2]:
entirename(fullName)
break
elif fullname[1]:
firstlast(fullName)
break
elif fullname[0]:
last(fullName)
break
main()
を。 –