当前位置 : 主页 > 编程语言 > python >

查看自己生日是否在pi中【Python习题】

来源:互联网 收集:自由互联 发布时间:2022-06-18
你好,我是悦创。 pi 是一个小数点后很多的数,以下给出查看自己的生日是否在 pi 中的实例: filename = 'pi_million_digits.txt' with open ( filename ) as file_object : lines = file_object . readlines () pi_s


你好,我是悦创。

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

直接就是数字而不是字符串


AI悦创·推出辅导班啦,包括「Python 语言辅导班、C++辅导班、算法/数据结构辅导班、少儿编程、pygame 游戏开发」,全部都是一对一教学:一对一辅导 + 一对一答疑 + 布置作业 + 项目实践等。QQ、微信在线,随时响应!




【文章原创作者:韩国服务器 https://www.68idc.cn 欢迎留下您的宝贵建议】
网友评论