###### tags: `Python` # 第四堂課練習-while迴圈 ## 三角形面積 ```python= a=int(input("請輸入三角形的底:")) b=int(input("請輸入三角形的高:")) c=a*b/2 #三角形算(底X高)/2 print("三角形面積 =%.2f" %(c)) ``` :::spoiler 執行結果 6.00 ::: ## 找出因數 ```=python n = int(input("請輸入一個數值:")) for i in range(1, n+1): if n % i == 0: print("因數:%d" %(i) ,end= ' ') #end=' '<<不跳行 print(",因數的倍數:",i*i) ``` :::spoiler 輸出結果 ![](https://i.imgur.com/ZNUfYR2.png) ::: ## while迴圈 ```=python i=1 while i <=3: print(i) i=i+1 ``` :::spoiler 輸出結果 ![](https://i.imgur.com/J2UgISv.png) ::: ## while判斷數值的位數 ```python= a=int(input("請輸入一個數值:")) i=1 while a>9: a=a//10 i +=1 print(i) ``` :::spoiler 輸出結果 ![](https://i.imgur.com/FJyQwS2.png) ::: 老師的寫法 ```python= a=int(input("請輸入一個數值:")) count=0 while a!=0: a=a//10 count +=1 print("%d 位數" %(count)) ```