def averageWordLength(mystring):
tempcount = 0
count = 1
wordcount = 0
try:
for character in mystring:
if character == " ":
tempcount +=1
if tempcount ==1:
count +=1
else:
tempcount = 0
try:
if character.isalpha(): #sorry for using the .isalpha
wordcount += 1
except:
wordcount = wordcount + 0
if mystring[0] == " " or mystring.endswith(" "): #i'm sorry for using the .endswith
count -=1
try:
result = wordcount/count
if result == 0:
result = "No words"
return result
else:
return result
except ZeroDivisionError:
error = "No words"
return error
except Exception:
error = "Not a string"
return errormystring =“你有什么大空间!”
输出是3.0,我没有使用拆分