计算字符串最后一个单词的长度,单词以空格隔开,字符串长度小于5000。(注:字符串末尾不以空格为结尾)
import sys for line in sys.stdin: a = line.split() print(len(a[-1]))
while True: # 写代码前记得加限定条件 try: in_str = input() if len(in_str) > 5000&nbs***bsp;len(in_str) == 0: raise Exception last = in_str.strip().split(" ")[-1] leng = len(last) print(leng) break except Exception: print("The length of str is invalid, please input it again!")