|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
答案:
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("所有的水仙花数分别是:", end='')
Narcissus()
自己打的:
def Narcissus():
for each in range(100,1000):
temp = each
sum = 0
while temp:
sum = sum + (temp%10) ** 3
temp = temp // 10
if sum == temp:
print(each, end='\t')
小白一枚,以上是我按小甲鱼给的答案一个一个打的,但是为什么引用的时候却没有这个函数,是哪步出错了吗?
因为在while循环中,temp 会一直被 10 地板除,直到为0,
后面的判断语句 if sum == temp: 永远不会成立
所以不会执行其中的打印语句
- 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')
- Narcissus()
复制代码
|
|