01月26日【Python3 基础知识】

5.1 九宫格
5.2 函数入门
5.3 判断某天为某年的第几天

5.1 九宫格

import random
x = 0
l = [1,2,3,4,5,6,7,8,9]
print("*************")
while len(l) != 0:
    n = int(random.random() * 100 % len(l))
#print(x)
#print(n)
    d = l.pop(n)
    print("|_{0}_".format(d),end="")
    x += 1
    if x == 3:
        print("|")
        x = 0
print("*************")

5.2 函数入门

# 函数
def a(args):
    pass
##################
def add(args):
    total = 0
    for i in args:
        total += i
    return total
def main():
    number = list()
    s = input("请输入(a + b + c + d + ...):")
    for num in s.split("+"):
        number.append(int(num.strip()))
    print(add(number))
if __name__ == '__main__':
    main()

5.3 判断某天为某年的第几天

a = {"1":31, "2":28, "3":31, "4":30, "5":31, "6":30, "7":31, "8":31,
     "9":30, "10":31, "11":30, "12":31}
b = {"1":31, "2":29, "3":31, "4":30, "5":31, "6":30, "7":31, "8":31,
     "9":30, "10":31, "11":30, "12":31}
days = 0
date = input("输入日期(1970-1-1):")
year, month, day = date.split("-")
if int(day) > 31:
    print("输入错误!")
elif int(month) > 12:
    print("输入错误!")
elif ((int(year) % 4 == 0) and (int(year) % 100 != 0)) or ((int(year) % 100 == 0) and (int(year) % 400 == 0)):
    if b[month] < int(day):
        print("输入错误!")
    else:
        for k, y in b.items():
            if int(k) < int(month):
                days += int(y)
        print("{0}是今年的第{1}天!".format(date, days + int(day)))
else:
    if a[month] < int(day):
        print("输入错误!")
    else:
        for k, y in a.items():
            if int(k) < int(month):
                days += int(y)
        print("{0}是今年的第{1}天!".format(date, days + int(day)))