20行代码,来测一测你的生日在不在“π”中

你是不是想知道自己的生日是不是在“π” 中呢,简单几行代码,见证你的奇迹!来人……放图!

代码是用python编写的,20来行代码♨♨ 话不多说,直接放图……额(⊙o⊙)…不,放代码……

try:
    with open("π.txt") as file:
        data = file.read()
except FileNotFoundError:
    print("文件不存在!")

else:
    data = data.strip()
    birthday = input("你的生日:")
    if len(birthday) != 4:
        print("请输入4位!")
        exit()
    if not birthday.isdecimal():
        print("请输入数字!")
        exit()

    if birthday in data:
        number = int(data.find(birthday))+1
        print("你的生日在圆周率的第%d位!" % number)

    else:
        print("很遗憾,你的生日不在圆周率的前1000000位中。")

本次小项目所涉及python知识并不多,但需要外部文档用于存放“π”值,可以去度娘那里要一份π的值存放在程序同一目录下的π.txt文档中。有兴趣的小伙伴一起来试一试吧!可以把你生日在π中的位数留言下方,看那些是有猿人哦O(∩_∩)O

猜你喜欢

转载自blog.csdn.net/qq_45807032/article/details/104806525
今日推荐