操作环境:Python版本,2.7.13;PyCharm版本,2017.1;电脑:Win 10 1703
1. 打印三角形
分为三角形,倒三角形和等腰三角形
#coding:utf-8
rows = int(raw_input("please input a number:"))
i = j = k = 1
print "直角三角形"
for i in range(0,rows):
for k in range(0,i + 1):
print "*",
k += 1
i += 1
print "\n"
rows = int(raw_input("please input a number:"))
print "直角三角形"
for i in range(0,rows):
for k in range(0,rows - i):
print "*",
k += 1
i += 1
print "\n"
print '等腰三角形'
for i in range(0,rows + 1):
for j in range(0,rows - i):
print " ",
j += 1
for k in range(0,2 * i - 1):
print "*",
k += 1
print "\n"
i += 1
2. 求1000以内的完全数有哪些
#coding:utf-8
list_num = []
num = 1000
for i in range(1,num + 1):
if sum([a for a in range(1,i) if i % a == 0]) == i:
list_num.append(i)
print i
print list_num
** 3. 打印万年历**
#coding:utf-8
#打印万年历
#1.判断是否为闰年
#2.计算每种情况下,每个月份的天数
#3.由2计算每年的天数
#4.计算每月一日对应的星期,基于1990年1月1日是星期一
class Wannianli():
year = int(raw_input("请输入查询的年份(如:2010):"))
month = int(raw_input("请输入查询的月份(如:3):"))
def _init_(self, year, month):
self.year = year
self.month = month
def is_or_not_leap_year(self):
if self.year % 4 == 0 and self.year % 100 == 0 or self.year % 400 == 0:
return True
else:
return False
def days_of_the_month(self):
big_m = [a for a in range(1,8) if a % 2 != 0] + [a for a in range(8,13) if a % 2 ==0]
small_m = [4, 6, 9, 11]
if self.month in big_m:
return 31
elif self.month in small_m:
return 30
elif self.is_or_not_leap_year():
return 29
else:
return 28
def days_between_19900101(self):
sum_days = 0
for y in range(1990, self.year + 1):
if self.is_or_not_leap_year():
sum_days += 366
else:
sum_days += 365
for m in range(1, self.month):
sum_days += self.days_of_the_month()
return sum_days
def the_firstday_of_month(self):
first_of_day = 1 + self.days_between_19900101() % 7
return first_of_day
def print_wnl(self):
print str(self.year) + " " + str(self.month)
print " "'星期一'" " '星期二'" "'星期三'" "'星期四'" "'星期五'" "'星期六'" "'星期日'
f = self.the_firstday_of_month()
# print ' * ' * (f - 1)
for p in range(1, self.days_of_the_month() + 1):
if f % 7 !=0:
if len(str(p)) == 1:
print ' ',
print ' ' + str(p),
print ' ',
else:
print ' ',
print ' ' + str(p),
print ' ',
else:
print ' '
print ' ' + str(p),
f = f + 1
if __name__ == '__main__':
WNL = Wannianli()
WNL.print_wnl()