看板 Python 關於我們 聯絡資訊
大家好 題目是假設給一個n 要print出小於n的square number 例如 n給25 則print出 1 4 9 16 我的code會把0也print 提示說就是寫一個while loop 所以我也就是照提示寫 如下: 還請各位高手幫忙 謝謝! import math n=int(input()) counter=0 while counter*counter<n: A=counter*counter if (A<0): continue counter = counter+1 counter+1 <math.sqrt(n) print(A) 我試圖想要讓print只給我大於0的結果 但沒有成功 -- -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 68.180.87.229 (美國) ※ 文章網址: https://www.ptt.cc/bbs/Python/M.1561300547.A.253.html
XperiaZ6C: A<0還是包含0啊,而且你何不counter從1開始 06/23 23:28
qwxr: A<=0 不就可以了嗎 06/23 23:31
qwxr: 差點誤導你 還得把counter自加轉到條件語句的上面 06/23 23:34
oToToT: 那個counter+1<math.sqrt(n)要幹嘛阿 06/24 01:03
AmigoSafin: counter從1開始就可以了(終於..)謝謝!! 回樓上 因為 06/24 11:16
AmigoSafin: 之前我的答案是從1開始 但包含n本身 所以也fail 06/24 11:16
yushes920179: 這邏輯滿亂的 建議你可以用for迴圈重寫 不要用while 07/04 22:36
AmigoSafin: 我今天又再寫一次 又包含n 所以我又回來看orz 07/08 01:31