文字列の最後の単語の長さ(Python,ファーウェイ試行)


目次
タイトルの説明
入出力説明
コードディスプレイ
コードウォーク
テストケース
1.複数の単語を入力
2.単語を入力
3.何も入力しない
トランスファゲート
 
タイトルの説明
文字列の最後の単語の長さを計算し、単語はスペースで区切られます.
 
入出力説明
入力:複数の単語.各単語はスペースで区切られています.例:Hello world
出力:最後の単語の長さ.例:5
 
コードディスプレイ
try:
    word_list = input().split()

    print(len(word_list[-1]))
except IndexError:
    print("Invalid words input.")

 
コードウォーク
 
try:
    #          ,     
    word_list = input().split()

    #              
    print(len(word_list[-1]))
#       ,  index  ,   IndexError  。     。
except IndexError:
    print("Invalid words input.")

 
テストケース
1.複数の単語を入力
introducing python
6

2.単語を入力
friends
7

3.何も入力しない
入力値が空の場合、コードの4行目に異常が発生し、取得されます.

Invalid words input.

 
トランスファゲート
str.split()メソッド
2.input()関数
https://blog.csdn.net/TCatTime/article/details/82556033
3.print()関数
https://blog.csdn.net/TCatTime/article/details/83450692