|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 Moshiro0830 于 2019-6-22 17:32 编辑
#寻找水仙花数
def WaterFlower(*number):
for i in number:
temp = i
sum = 0
while temp :
sum = sum + ( temp % 10) ** 3
temp = temp // 10
if sum == i:
print( i , end = '\t')
print('所有的水仙花数分别是:', end = '')
WaterFlower(152,153,154,155,369,370,371,372)
print('\n\n*******\t\t\t小甲鱼\t\t\t*******')
def Narcissus():
for each in range(100, 1000):
temp = each
sum = 0
while temp:
sum = sum + (temp%10) ** 3
temp = temp // 10 # 注意这里用地板除
if sum == each:
print(each, end='\t')
print("\n所有的水仙花数分别是:", end='')
Narcissus()
输出结果如下:
所有的水仙花数分别是:153 370 371
******* 小甲鱼 *******
所有的水仙花数分别是:153 370 371 407
==============分割线=================
*number 表示收集参数,不确定参数的个数。
print(range(100,1000)) 会报错,原因如下:
得出的结果是 range(0,10) ,而不是[0,1,2,3,4,5,6,7,8,9] ,为什么呢?
官网原话:
In many ways the object returned by range() behaves as if it is a list, but in fact it isn’t. It is an object which returns the successive items of the desired sequence when you iterate over it, but it doesn’t really make the list, thus saving space.
We say such an object is iterable, that is, suitable as a target for functions and constructs that expect something from which they can obtain successive items until the supply is exhausted. We have seen that the for statement is such an iterator. The function list() is another; it creates lists from iterables:
翻译:
可以看到上面这个很奇怪,在很多种情况下,range()函数返回的对象的行为都很像一个列表,但是它确实不是一个列表,它只是在迭代的情况下返回指定索引的值,但是它并不会在内存中真正产生一个列表对象,这样也是为了节约内存空间。
我们称这种对象是可迭代的,或者是可迭代对象,还有一种对象叫迭代器,它们需要从一个可迭代对象中连续获取指定索引的值,一直到索引结束。list()函数就是这样一个迭代器,它可以把range()函数返回的对象变成一个列表。
总结:
range() 函数返回的是一个可迭代对象(类型是对象),而不是列表类型, 所以打印的时候不会打印列表。
list() 函数是对象迭代器,把对象转为一个列表。返回的变量类型为列表。
所以结合list(),可以使用print(list(range(100,1000)))打印出[100,101...999]
但是WaterFlower(list(range(100,1000)))却会报错,有什么办法可以将range(100,1000)赋给number,正确输出?
- def WaterFlower(*number):
- for i in number:
- temp = i
- sum = 0
- while temp :
- sum = sum + ( temp % 10) ** 3
- temp = temp // 10
- if sum == i:
- print( i , end = '\t')
-
- print('所有的水仙花数分别是:', end = '')
- WaterFlower(*range(100,1000))
复制代码
|
|