3月14日 我用Python几十行代码为女朋友画了一个爱心

今天是个特殊的日子,圆周率日,哈哈!来对你爱的人表达爱吧!
女朋友就是我爱人啦!😁

# -*- coding:utf-8 -*-
# @Python Version: 3.7
# @Time: 2020/3/14 13:14
# @Author: Michael Ming
# @Website: https://michael.blog.csdn.net/
# @File: Valentine'sDay.py
# @Reference: 
import matplotlib.pyplot as plt
import numpy as np
from decimal import Decimal
from decimal import getcontext
import time


def heartfunc(x, y):
    return (x ** 2 + y ** 2 - 1) ** 3 - x ** 2 * y ** 3 <= 0


def cal_pi(precision):
    getcontext().prec = precision
    return sum(1 / Decimal(16) ** k *
               (Decimal(4) / (8 * k + 1) -
                Decimal(2) / (8 * k + 4) -
                Decimal(1) / (8 * k + 5) -
                Decimal(1) / (8 * k + 6)) for k in range(precision))


def printer(text, delay=0.1314):
    """打字机效果"""
    for ch in text:
        print(ch, end='', flush=True)
        time.sleep(delay)


if __name__ == '__main__':
    n = 1314
    x = np.linspace(-2, 2, n)
    y = np.linspace(-2, 2, n)
    X, Y = np.meshgrid(x, y)
    plt.contourf(X, Y, heartfunc(X, Y), cmap=plt.cm.autumn)
    # 颜色查询 https://matplotlib.org/examples/color/colormaps_reference.html
    plt.title("5201314")
    plt.show()

    loveInPi = str(cal_pi(1314))
    heart = ['5', '2', '0', '1', '3', '1', '4']
    iloveyou = "5201314"
    love = ""
    i, j = 0, 0
    while love != iloveyou:
        if loveInPi[i] == heart[j]:
            love += loveInPi[i]
            j += 1
        i += 1
    printer("Michael在圆周率中找到了爱的誓言:" + love + " to my love!")
Michael在圆周率中找到了爱的誓言:5201314 to my love!

在这里插入图片描述

爱是恒久忍耐,又有恩慈;爱是不嫉妒;爱是不自夸,不张狂,
Love is patient, love is kind. It does not envy, it does not boast, it is not proud.
不做害羞的事,不求自己的益处,不轻易发怒,不计算人的恶,
It does not dishonor others, it is not self-seeking, it is not easily angered, it keeps no record of wrongs.
不喜欢不义,只喜欢真理;
Love does not delight in evil but rejoices with the truth.
凡事包容,凡事相信,凡事盼望,凡事忍耐。
It always protects, always trusts, always hopes, always perseveres.
爱是永不止息。 (哥林多前书 13:4-8 )
Love never fails. (Corinthians 13:4-8 NIV)

本文参考:

Python 必杀技:用 print() 函数实现的三个特效
一个神奇的公式计算Pi的任意位数

©️2020 CSDN 皮肤主题: 代码科技 设计师: Amelia_0503 返回首页
实付0元
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值