|
|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
题目要求:如果一个3位数等于其各位数字的立方和,则称这个数为水仙花数。例如153 = 1^3+5^3+3^3,因此153是一个水仙花数。编写一个程序,找出所有的水仙花数。
答:
#寻找水仙花数
def shuixianhua()
result = 0
list1 = []
for each in range(100,1000):
t = each
while t :
r = t % 10
t //= 10
result = result + r**3
if each == temp:
list1.append(each)
print(list1)
shuixianhua()
请问这样为什么会打印空列表呢
应该将代码这样改:
- def shuixianhua():
- list1 = []
- for each in range(100, 1000):
- result = 0
- t = each
- while t:
- r = t % 10
- t //= 10
- result = result + r ** 3
- if each == result:
- list1.append(each)
- print(list1)
- shuixianhua()
复制代码
|
|