实现环境:Python 3:
1.函数入门实例
#/usr/bin/env python
# -*- coding:utf-8 -*-
# @time :2018/1/26 21:37
# @Author :FengXiaoqing
# @file :Hanshu.py
def add(args):
total = 0
for i in args:
total += i
return total
def main():
number = list()
s = input("Please input some number add (a + b + c ..):")
print(s)
for num in s.split("+"):
number.append(int(num.strip("+")))
print(add(number))
if __name__ == ‘__main__‘:
main()
2. 九宫格实例
#/usr/bin/env python
# -*- coding:utf-8 -*-
# @time :2018/1/26 20:55
# @Author :FengXiaoqing
# @file :JiuGongGe.py
number = list()
for i in range(1,10):
number.append(i)
for A in number:
a = list()
for i in range(1,10):
a.append(i)
a.remove(A)
for B in a :
b = list()
for i in a:
b.append(i)
b.remove(B)
for C in b:
c = list()
for i in b:
c.append(i)
c.remove(C)
for D in c:
d = list()
for i in c:
d.append(i)
d.remove(D)
for E in d:
e = list()
for i in d:
e.append(i)
e.remove(E)
for F in e:
f = list()
for i in e:
f.append(i)
f.remove(F)
for G in f:
g = list()
for i in f:
g.append(i)
g.remove(G)
for H in g:
h = list()
for i in g:
h.append(i)
h.remove(H)
for I in h:
if (A+B+C) == (D+E+F) == (G+H++I) == (A+D+G) == (B+E+H) == (C+F+I) == (A+E+I) == (C+E+G):
print(‘‘
-----------------
|_{0}_|_{1}_|_{2}_|
|_{3}_|_{4}_|_{5}_|
|_{6}_|_{7}_|_{8}_|
‘‘.format(A,B,C,D,E,F,G,H,I))
答案:
_____________
|_2_|_7_|_6_|
|_9_|_5_|_1_|
|_4_|_3_|_8_|
_____________
|_2_|_9_|_4_|
|_7_|_5_|_3_|
|_6_|_1_|_8_|
_____________
|_4_|_3_|_8_|
|_9_|_5_|_1_|
|_2_|_7_|_6_|
_____________
|_4_|_9_|_2_|
|_3_|_5_|_7_|
|_8_|_1_|_6_|
_____________
|_6_|_1_|_8_|
|_7_|_5_|_3_|
|_2_|_9_|_4_|
_____________
|_6_|_7_|_2_|
|_1_|_5_|_9_|
|_8_|_3_|_4_|
_____________
|_8_|_1_|_6_|
|_3_|_5_|_7_|
|_4_|_9_|_2_|
_____________
|_8_|_3_|_4_|
|_1_|_5_|_9_|
|_6_|_7_|_2_|
3. 判断某天为某年的第几天实例
#/usr/bin/env python
# -*- coding:utf-8 -*-
# @time :2018/1/26 21:53
# @Author :FengXiaoqing
# @file :dateTime.py
from datetime import datetime # 这样才是把datetime中的datetime类给导入
y = int(input(‘请输入4位数字的年份(如:2018):‘)) # 获取年份
m = int(input(‘请输入月份(如:05):‘)) # 获取月份
d = int(input(‘请输入是哪一天(如:12):‘)) # 获取“日”
dt = datetime(y, m, d)
print("您输入的日期是{0}第".format(y)+dt.strftime("%j")+"天")
原文地址:http://blog.51cto.com/fengyunshan911/2065691
时间: 2024-10-09 02:10:29