猜数字游戏:
i=random.randint(1,101)
while True:
a=int(input('请输入一个数字:'))
if i>a:
print('你猜小了')
elif i<a:
print('你猜大了')
else:
print("你猜对了")
打印出1-100的偶数和,奇数和:
n=1;sum=0;jishu=0;
while n<=100:
if n%2==0:
sum+=n;
else:
jishu+=n;
n+=1;#迭代部分
print("1-100之间的偶数和是:"+str(sum))
print("1-100之间的奇数和是:"+str(jishu))
输出1-100中3的倍数,5的倍数及3和5的公倍数
n=1;
while n<=100:
if n%3==0 and n%5==0:
print("{0}是3和5的倍数".format(n))
elif n %3==0:
print("{0}是3的倍数".format(n))
elif n%5==0:
print("{0}是5的倍数".format(n))
n+=1;
else:
print("循环完毕,判断结束")
菱形:
for i in range(1,10,2):
print(("*"*i).center(20," "))
for j in range(7,0,-2):
print(("*"*j).center(20,' '))
乘法表:
for i in range(1,10):
for j in range(1,i+1):
print(j,"*",i,"=",i*j,end="\t")
print()