17 lines
840 B
Python
17 lines
840 B
Python
|
#程式接受單列多筆輸入,把輸入0之前的數值加總,忽略非數字的輸入內容
|
|||
|
|
|||
|
inputList = input("*** 將加總輸入的數字,但忽略非數字的資料 ****\n\n請輸入多筆資料再按<Enter> => ").split()
|
|||
|
|
|||
|
i, inputSum = 0, 0
|
|||
|
while i < len(inputList):
|
|||
|
print("\n輸入值為",inputList[i])
|
|||
|
j = 1 if inputList[i][0]=='-' else 0
|
|||
|
if inputList[i][j:].isnumeric(): #.isnumeric()判斷字串是否全由數字組成,若是,回傳True,否則回傳False;注意:負號不視為數字
|
|||
|
#isnumeric()的用法可參見 https://www.w3schools.com/python/ref_string_isnumeric.asp
|
|||
|
#字串轉數字的他種寫法,可用try-except架構(即將教到)
|
|||
|
inputSum += eval(inputList[i])
|
|||
|
if inputList[i]=="0": i += len(inputList)
|
|||
|
i += 1
|
|||
|
|
|||
|
print("\n您剛剛輸入0囉,有效輸入總和 =",inputSum,"Bye ~")
|