利用python来进行田字格,计算π值等简单程序的编写,适合新手小白尝试,本人也是新手小白一个,代码经过本人运行测试通过。
#田字格
scale = int(input("输入边长大小:"))
if(scale%2==0):scale+=1
x=scale-1
for i in range(scale):
if(i in[0,x//2,x]):print("+{0:-^{1}}+".format("+",scale))
else:print("|{0: ^{1}}|".format("|",scale))
#填充——,空格为全角
测试运行
其中DARYS值越大,计算结果越精确,但运算时间变长
from random import random
from math import sqrt
import time
DARYS = 10000000
hits = 0.0
time.perf_counter()
for i in range(1,DARYS+1):
x,y = random(),random()
dist = sqrt(x**2+y**2)
# print("xxx运行时间是:{:.5f}s".format(time.perf_counter()))
if dist <=1.0:
hits = hits+1
pi = 4*(hits/DARYS)
print("pi值是{}.".format(pi))
print("运行时间是:{:.5f}s".format(time.perf_counter()))
测试结果
当DARYS = 10000000 运行时间4.7s
当DARYS = 100000000 运行时间44s
from random import*
define,i,guess = randint(0,100),0,0
while(1):
i+=1
guess =(input("请输入:"))
if(guess.isnumeric()):
guess = int(guess)
if(define>guess): a ="遗憾,太小了"
elif(define