Python程序——梅花易数中的时占

基于字典和列表实现 梅花易数

主要实现梅花易数中的 时占法:

Python程序——梅花易数中的时占_第1张图片
导入了datetime模块获取当前时间
计算天干,地支和时辰
展示初卦,互卦和变卦的基本卦象

也做了简单的卦辞的json,可以导入以显示卦辞
仅用于学习研究!

# -*- coding: utf-8 -*-
# @Author  : LuoXian
# @Date    : 2020/2/8 23:39
# Software : PyCharm
# version: Python 3.8
# @File    : 时占.py
import datetime

twelve = {
    1: ['乾', [1, 1, 1]],
    2: ['兑', [0, 1, 1]],
    3: ['离', [1, 0, 1]],
    4: ['震', [0, 0, 1]],
    5: ['巽', [1, 1, 0]],
    6: ['坎', [0, 1, 0]],
    7: ['艮', [1, 0, 0]],
    8: ['坤', [0, 0, 0]],
}
dict_move = {
    str([1, 1, 1]): '乾',
    str([0, 1, 1]): '兑',
    str([1, 0, 1]): '离',
    str([0, 0, 1]): '震',
    str([1, 1, 0]): '巽',
    str([0, 1, 0]): '坎',
    str([1, 0, 0]): '艮',
    str([0, 0, 0]): '坤'
}
# 先天八卦数:乾 1 兑 2 离 3 震 4 巽 5 坎 6 艮 7 坤 8 

# 获取当前时间
now = str(datetime.datetime.today())
year = int(now[:4])
mouth = int(now[5:7])
day = int(now[8:10])
time = int(now[11:13])


# 天干,地支,时辰
def format_time():
    tiangan = (year - 3) % 10
    dizhi = (year - 3) % 12
    if time >= 23 or time < 1:
        shichen = 1
    elif 1 <= time < 3:
        shichen = 2
    elif 3 <= time < 5:
        shichen = 3
    elif 5 <= time < 7:
        shichen = 4
    elif 7 <= time < 9:
        shichen = 5
    elif 9 <= time < 11:
        shichen = 6
    elif 11 <= time < 13:
        shichen = 7
    elif 13 <= time < 15:
        shichen = 8
    elif 15 <= time < 17:
        shichen = 9
    elif 17 <= time < 19:
        shichen = 10
    elif 19 <= time < 21:
        shichen = 11
    else:
        shichen = 12
    get_num(dizhi, shichen)


# 获取卦数
def get_num(dizhi, shichen):
    up = (dizhi + mouth + day) % 8 if (dizhi + mouth + day) % 8 else 8
    down = (dizhi + mouth + day + shichen) % 8 if (dizhi + mouth + day + shichen) % 8 else 8
    move = (dizhi + mouth + day + shichen) % 6 if (dizhi + mouth + day + shichen) % 6 else 6
    show(up, down, move)


# 打印结果
def show(up, down, move):
    result = twelve[up] + twelve[down]
    all_num = result[1] + result[3]
    # 初卦
    result1 = result[0] + result[2]
    # 互卦
    hu_up = str(all_num[1:4])
    hu_down = str(all_num[2:5])
    result2 = dict_move[hu_up] + dict_move[hu_down]
    # 变卦
    all_num[move - 1] = abs(all_num[move - 1] - 1)
    move_up = str(all_num[:3])
    move_down = str(all_num[3:])
    result3 = dict_move[move_up] + dict_move[move_down]
    # 体 用
    if move < 4:
        use = '。'
    else:
        use = '  ' + '。'

    # 打印
    input(f'{now[:19]}\n无动不占(时占):')
    print(f'\n\t卦象:{result1}')
    print(f'\t用卦:{use}')
    print(f'\n\t互见:{result2}')
    print(f'\n\t动幺:{move}')
    print(f'\t变卦:{result3}')


if __name__ == '__main__':
    format_time()

你可能感兴趣的:(Python程序)