大家好~
不好意思又来打扰
我今天忙完其他作业
又打开练习 重新写一次
如下:
n=int(input())
counter=0
while counter*counter<n:
counter=counter+1
print(counter*counter)
但我的答案都会包含最后一个数值
例如n是36
我的答案都会包含36
但题目要的答案是 1 4 9 16 25
我的会多一个
思考了大约一小时还没想出我的问题出在哪?
请问各位前辈我是否遗忘了什么?
至于为何用while
因为给我的范例是用while
我也觉得改起来还蛮好理解的
我是蛮疑惑我明明写counter*counter<n
是否该加一个 if ((counter+1)*(counter+1)=n): break ?
谢谢大家!
※ 引述《AmigoSafin ()》之铭言:
: 大家好
: 题目是假设给一个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的结果
: 但没有成功