Python编程题(三)

Python编程题(三),第1张

demo37:

import random
c = random.randint(0,100)
print("请您猜猜电脑的数字!")
while True :
	p = int(input("请输入您的数字:"))
	if p > c :
		print("大了")
	elif p < c :
		print("小了")
	else :
		print("恭喜你答对了!")
		break		

 

demo38:

 

​
n1,n2 = eval(input("请输入两个数"))
if n1 > n2 :
	little = n2
elif n1 < n2 :
	little = n1
else :
	print("最大公约数为:"+str(n1))
	exit(0)
for i in range(1,little + 1) :
	if n1 % i == 0 and n2 % i == 0:
		GCD = i
print("最大公约数为:%s"%GCD)

​

demo39:

 

 

import random
import math
n = 1
numberOfHits = 0
while n <= 1000000 :
	x = random.uniform(0,1)
	y = random.uniform(0,1)
	length = math.sqrt(x**2 + y**2)
	if length <= 1 :
		numberOfHits += 1
	n += 1
PI = 4 * numberOfHits / 1000000
print("π的值为:",PI)

demo40: 

等着教我写

demo41:

 

height = int(input("需要的打印金字塔的高度:"))
for i in range(0,height) :
    empty = height - i - 1
    for j in range(0,empty) :
        print('  ',end = "")
    number = 2 * (i + 1) - 1
    left = i + 1
    for k in range(left,0,-1) :
        print(k,end = " ")    
    for k in range(2,left + 1) :
        print(k,end = " ")
    print('')

 

 demo42:

line = int(input("请输入几行:"))
for i in range(0 , line) :
    for j in range(0 , i + 1) :
        print(j + 1,end = " ")
    print('')
print('')
for i in range(0 , line) :
    for j in range(0 , line - i) :
        print(j + 1,end = " ")
    print('')
print('')
for i in range(0 , line) :
    kong = line - i - 1
    for j in range(0 , kong) :
        print('  ',end = "")
    for j in range(i + 1 , 0 , -1) :
        print(j,end = " ")
    print('')
print('')
for i in range(0 , line) :
    for j in range(0 , i) :
        print('  ',end = "")
    for j in range(0 , line - i) :
        print(j + 1,end = " ")
    print('')

 

 demo43:

 

line = int(input("请输入几行"))
for i in range(0 , line) :
    kong = line - i - 1
    for j in range(0 , kong) :
        print('    ',end = "")
    left_number = i + 1
    for j in range(1 , left_number + 1) :
        if 2 ** (j - 1) < 10 :   #if控制一位和两位三位数字之间的间隔
            print(2 ** (j - 1),end = "   ")
        elif 10 <= 2 ** (j - 1) < 100:
            print(2 ** (j - 1),end = "  ")
        else :
            print(2 ** (j - 1),end = " ")
    for j in range(0 , i) :
        x = (2 ** i) * (0.5 ** (j + 1)) #金字塔右边数字
        x = int(x)
        if x < 10 :
            print(x,end = "   ")
        elif 10 <= x < 100 :
            print(x,end = "  ")
        else :
            print(x,end = " ")
    print('')

demo44:

 

 

 = 0
time = 0
for i in range(2 , 1001) :
    for j in range(1 , i + 1) :
        if i % j == 0 :
            n += 1
    if n == 2 :
        print(i,end = " ")
        time += 1
        if time % 8 == 0 :
            print('')
    else :
        n = 0

 demo45:

s = 0
i = int(input("请输入i的值:"))
for j in range(0 , i) :
    s += 1 / (1 + 2 * j)
    s *= -1
print("π的近似值:"+str(4 * s))

 

 deno46;

s = 1
e = 0
time = 1
i = int(input("请输入次数:"))
while time <= i :
    for j in range(time) :
        s = s * (j + 1)
    time += 1
    e = 1 / s
    s = 1
print("e的近似值是:",e)

 

 

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/723532.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-04-26
下一篇 2022-04-26

发表评论

登录后才能评论

评论列表(0条)

保存