Python零基礎學習代碼實踐 —— 提取字符串裏面的單詞數


str = input()
str1 = str.strip()
index = 0
count = 0
while index < len(str1):
    while str1[index] != " ":
        index += 1
        if index == len(str1):
            break
    count += 1
    if index == len(str1):
        break
    while str1[index] == " ":
        index += 1
print(count)


執行結果

C:\Users\Administrator\AppData\Local\Programs\Python\Python37-32\python.exe C:/Users/Administrator/PycharmProjects/untitled/day005/提取字符轉裏面的單詞數.py

4

Process finished with exit code 0


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章