python 循环练习

Demo37        循环猜0--100的数字根据提示猜对为止

import random
sum = int(input("请输入一个0到100的整数包括0和100:"))
a = random.randint(0,100)
while sum != a:
	if sum > a:
		print("数字过大请输入一个更小的数:")
		sum = int(input("请输入一个0到100的整数包括0和100:"))
		

	elif sum < a:
		print("数字过小请输入一个更大的数:")
		sum = int(input("请输入一个0到100的整数包括0和100:"))
		
else :
	print("你猜对了")

python 循环练习_第1张图片 

 

Demo38        找两个整数最大公约数

(n1,n2) = eval(input("请输入两个整数:"))
gcd = 0
k = 2
if n1 ==n2 :
	print("最大公约数是1")
elif n1 != n2 :
	if n1 > n2 :
		while k <= n2 :
			if n1 % k == 0 and n2 % k == 0 :
				gcd = k
				k += 1
			else :
				k +=1
		print("(%s,%s)的最大公约数是:%s"%(n1,n2,gcd))
			

		
	if n1 < n2 :
		while k <= n1 :
			if n1 % k == 0 and n2 % k == 0 :
				gcd = k
				k += 1
			else :
				k += 1
		print("(%s,%s)的最大公约数是:%s"%(n1,n2,gcd))		


	

python 循环练习_第2张图片 

 

Demo39        蒙卡罗模拟--圆周率

import math
import random
i = 0
numberhits = 0

while i < 1000000:
	x = random.random()*2-1
	y = random.random()*2-1
	i += 1
	if math.sqrt(x**2+y**2)<=1.0:
		numberhits += 1
	else:
		numberhits=numberhits
	continue	
else:
	pi = 4*numberhits/1000000
	print(pi)

python 循环练习_第3张图片 

 

Demo40        统计正负数个数并计算平均值

num = int(input("输入一个整数:"))
i = 0
j = 0
k = 0
num1 = 0
num2 = 0
sum1 = 0
sum2 = 0
everage = 0
while num != 0:
	if num > 0:
		sum1 += num
		j +=1
	if num < 0:
		sum2 += num
		k +=1
	num = int(input("输入一个整数:"))
else:
	i = j + k 
	sum = sum2 + sum1 
	everage = (sum2+sum1)/i
	print("正数个数为:%d"%j)
	print("负数个数为:%d"%k)
	print("总个数为:%d"%sum)
	print("平均数值为:%s"%everage)

	

python 循环练习_第4张图片 

 

Demo41        找一个整数的所有因子


sum = int(input("输入一个整数:"))
n = 2
while True:
	if sum % n == 0:
		print(n,end = " , ")
		sum /= n 
	else :
		n += 1
	if sum == 1:
		break




python 循环练习_第5张图片 

 

Demo42        显示一个数字金字塔

# 43两个组合
num = int(input('输入层数:'))
for  i in range(1,num+1):
	for j in range(0,num-i):
		print(" ",end = "")
	for j in range(i,0,-1):
		print(j,end = "")
	for j in range(2,i+1):
		print(j,end ="")

	
	print("")

python 循环练习_第6张图片 

 

Demo43        循环显示四种模式



sum = int(input("输入层数:"))
print(" ")

print("#模式A")
print(" ")
for a in range(1,sum+1):
	for b in range(1,a+1):
		print(b,end = "")
	print("")
print(" ")

print("#模式B ")
print(" ")
for c in range(1,sum+1):
	for d in range(1,sum+2-c):
		print(d,end = "")
	print("")

print("#模式C")
print(" ")
for c in range(1,sum+1):
	for d in range(1,sum+1-c):
		print(" ",end = "")
	
	for d in range(c,0,-1):#一行里 n 到 0 ,以n-1,n-2,…… ,0
		print(d,end = "")
	print("")
print(" ")

print("#模式D")
print(" ")
for c in range(1,sum+1):
	for d in range(2,c+1):
		print(" ",end = "")
	
	for d in range(1,sum+2-c):
		print(d,end = "")

	print("")

python 循环练习_第7张图片 

 

Demo44        在金字塔模拟中显示数字

sum = int(input("请输入数字层数:"))
for i in range(1,sum+1):
	for j in range(0,sum-i):
		print(" ",end ="")
	for j in range(0,i):
		if j == i-1 and j != i-2:
			print(" %d"%(2**j ) ,end ="")
		else:
			print("%d"%(2**j ) ,end ="")
	for j in range(i,1,-1):
		print("%d"%(2**(j-2))  ,end ="")
	print("")

python 循环练习_第8张图片 

 

Demo45        显示2--100的素数

n = 0
for x in range(2,1001):
    for i in range(2,x):
        if x % i == 0:
            break
    else:
        print("%d  "%x, end ="")
        n += 1
        if n % 8 == 0:
            print("\n")

python 循环练习_第9张图片 

 

Demo46 -- 50

demo51    十进制到二进制

# 10 进制转换为 2 进制
a = int(input("请输入一个十进制数:"))
list = ''
while a > 0:
	list += str(a % 2)
	a = a // 2
print("二进制数:%s"%list[::-1])

python 循环练习_第10张图片 

 

 

Demo52

Demo53    蒙特卡罗模拟

import random
i = 0
numhits = 0
while i < 1000000:
	
	x = random.random()*2-1
	y = random.random()*2-1
	i += 1
	if -1

python 循环练习_第11张图片 

 

Demo54  显示闰年(2001 -- 2100)

x = 2001
i = 0
while x<2101:
	
	if x % 4 == 0 and x % 100 != 0 or x % 400 == 0:
		print("%d  "%x,end = "")
		x += 1
		i += 1
		if i % 8 == 0:
			print("\n")
		continue
	else:
		x +=1
		continue

 

python 循环练习_第12张图片 

 

你可能感兴趣的:(python)