一、问题背景
“Python语言导论“课程作业:
1. 从键盘输入一个整数,判断该数字能否被2和3同时整除,能否被2整除,能否被3整除,不能被2和3整除。输出相应信息。
2. 打印出所有的“水仙花数”,所谓“水仙花数”是指一个3位数,其各位数字立方和等于该数本身。例如,153是一个水仙花数,因为153=13+53+33。
二、实现环境
Window 8,32位系统,Python 3.5 IDLE。
三、代码
1. 整除
'''
Author: WJT
Date: 10/1/2016
Iuput: an integer
decide whether the integer can be diveded by 2 or 3
'''
a = input("Please input an integer\n");
a = int(a);
if(a % 2 == 0):
if(a % 3 == 0):
print(a, " can be diveded by 2 and 3");
else:
print(a, " only can be diveded by 2");
else:
if(a % 3 == 0):
print(a, " only can be diveded by 3");
else:
print(a, " can't be diveded by 2 or 3");
2.水仙花数
''' Author: WJT Date: 10/1/2016 find out all narcissistic numbers ''' from math import *; print("all the narcissistic numbers:"); for i in range(100, 1000): hundred = floor(i / 100); ten = floor(i % 100 / 10) one = floor(i % 10); if(i == pow(hundred, 3) + pow(ten, 3) + pow(one, 3)): print(i);