程序纠正错误问题
大概的意思就是通过一系列函数找到了具有特征的一连串数字放在了列表里,函数什么的都不用看,问题在下列程序#后面,不明白为什么不能有 lst =def isUglyNumber(x):
if x == 1:
return True
else:
for i in :
while x%i == 0:
x //= i
return x==1
def getUglyNumber(N,lst):
count = 0
x = 1
while count<N:
if isUglyNumber(x):
lst.append(x)
count += 1
x += 1
if __name__ == "__main__":
N = int(input("Please enter the N(N>0): "))
lst = []
lst = getUglyNumber(N,lst) #为什么要把”lst =“给去掉呢
print(lst)
with open("out.txt","w") as fp:
fp.write(str(lst))
是否加等号主要取决于函数是否有返回值 return
比如说 getUglyNumber(N,lst) 的内容是给它的第二个参数做append操作
而非返回一个新的lst列表
如果你在函数内部新建一个列表并返回,可以给lst赋值
def getUglyNumber(N):
lst = []
count = 0
x = 1
while count<N:
if isUglyNumber(x):
lst.append(x)
count += 1
x += 1
return lst
页:
[1]