37.猜数字游戏(0~100),猜猜电脑里存储的数字?
import random
b = random.randint(0,100)
c = 0
while(c == 0):
a = int(input("请输入0~100之间的任一整数(包括0和100):"))
if a == b:
print("是的,你猜对了!")
c += 1
elif a < b:
print("你猜的数字小了")
elif a > b:
print("你猜的数字大了")
?38.两个整数间的最大公约数
a = int(input("请输入第一个数字:"))
b = int(input("请输入第一个数字:"))
m = max(a, b)
n = min(a, b)
r = m % n
while r != 0:
m = n
n = r
r = m % n
print(a, "和", b, "的最大公约数为", n)
40.?
import random
num = []
s = 0
b = 0
c = 0
a = int(input("请输入一个数,如果停止输入请输入0:"))
s += a
num.append(a)
if a>0:
b += 1
elif a<0:
c += 1
while a != 0:
a = int(input("请输入一个数,如果停止输入请输入0:"))
s += a
num.append(a)
if a>0:
b += 1
elif a<0:
c += 1
continue
else:
print("正数的个数为:",b)
print("负数的个数为:",c)
ave = s / (len(num)-1)
print("这几个数的和为:%d"%s)
print("这几个数的平均值为:%.2f"%ave)
42.数字金字塔
a = int(input("输入打印的行数:"))
for i in range(1,a+1):
b = a - i
for t in range(0,b):
print(' ', end='')
for j in range(1,i+1):
print(i-j+1,end='')
if (i-j+1)==1:
if i!=1:
for k in range(2,i+1):
print(k,end='')
print()
43.数字三角形
#打印以下图形
"""
1
12
123
1234
12345
123456
"""
a = int(input("输入打印的行数:"))
b = 1
while b <= a:
j = 1
while j <= b:
print(j,end='')
j += 1
print()
b += 1
"""
123456
12345
1234
123
12
1
"""
a = int(input("输入打印的行数:"))
for i in range(a,0,-1):
for j in range(1,i+1):
print(j,end='')
print("")
"""
1
21
321
4321
54321
654321
"""
a = int(input("输入打印的行数:"))
for i in range(1,a+1):
b = a - i
for t in range(0,b):
print(' ', end='')
for j in range(1,i+1):
print(i-j+1,end='')
print()
"""
123456
12345
1234
123
12
1
"""
a = int(input("输入打印的行数:"))
for i in range(a,0,-1):
b = a - i
for j in range(0,b):
print(' ', end='')
for j in range(1,i+1):
print(j,end='')
print("")
45.显示在2~1000之间的素数
a = []
for i in range(2, 1000):
for j in range(2, i//2+1):
if i % j == 0:
break
else:
a.append(i)
for k in range(len(a)):
print(a[k], end=' ')
if (k+1) % 8 == 0:
print("\n")
49.游戏剪刀石头布,只要一方连胜两局就退出游戏
import random
flag1 = 0
flag2 = 0
while flag1<2 and flag2<2:
b = int(input("请输入一个数字0(剪刀)、1(石头)或2(布):"))
a = random.randint(0,2)
if a == b:
print("平局")
if (a == 0 and b == 1)or(a == 1 and b == 2)or(a == 2 and b == 0):
print("恭喜你赢了!")
flag1 += 1
if (a == 1 and b == 0)or(a == 2 and b == 1)or(a == 0 and b == 2):
print("你输了!")
flag2 += 1
if flag1<2 and flag2<2:
print("再来")
elif flag1==2:
print("游戏结束,你赢了")
elif flag2==2:
print("游戏结束,你输了")
else:
print("游戏结束,平局")
51.十进制转为二进制
i = int(input("请输入一个十进制整数:"))
a = []
while i:
a.append(i % 2)
i = i // 2
a.reverse()
for j in range(0,len(a)):
print(a[j])
54.显示2001年到2100年的闰年
b = []
for a in range(2001,2101):
if a%4==0 and a%100!=0 or a%400==0:
b.append(a)
for i in range(len(b)):
print(b[i], end=' ')
if (i+1) % 10 == 0:
print("\n")
|