查看自己生日是否在pi中

pi是一个小数点后很多的数,以下给出查看自己的生日是否在pi中的实例

filename = 'pi_million_digits.txt'

with open(filename) as file_object:
    lines = file_object.readlines()

pi_string = ''
for line in lines:
    pi_string += line.rstrip()

birthday = input("Enter your birthday, in the form mmddyy: ")
if birthday in pi_string:
    print("Your birthday appears in the first million digits of pi!")
else:
    print("Your birthday does not appear in the first million digits of pi.")
print(pi_string[:10])

上面代码是书中作者给出的

以下为笔者笔记,稍微复杂,加入了是否继续执行的命令

filepath = r'C:\Users\Wudl\Desktop\pcc-master\chapter_10\pi_million_digits.txt'

with open(filepath) as file:
    lines = file.readlines()

pi = ''
for line in lines:
    pi += line.strip()
print(pi[:10])#1

while True:
    age = input('Please enter your birthday>>>')
    if age in pi:
        print('Your birthday in book')
        dicision = input('Do you want to continue?y/n>>>')
        if dicision == 'n':
            break
    else:
        print('No')
        dicision = input('Do you want to continue?y/n>>>')
        if dicision == 'n':
            break

详细:
1print(pi[:10])给出的结果是:
3.14159265
直接就是数字而不是字符串

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值