odd=0
even=0
index=0
while index<=100:
if index & 1 ==0:
even+=index
else:
odd+=index
index +=1
print(f"0~100的偶数和是{even}")
print(f"0~100的奇数和是{odd}")
结果:
count = 0
index = 1
while index <=100:
count +=index
index += 1
print(f"0~100的和是{count}")
结果:
count = 1
index = 1
while index <=100:
count *=index
index += 1
print(f"0~100的积是{count}")
注:在python中即是运算的结果很大也会打印输出 结果:
index = 0
while index <=20:
index += 1
if index ==9:
break
print(index)
else:
print("循环正常结束后,才会执行的代码")
print("循环后的代码")
结果:
index = 0
while index <=20:
index += 1
if index ==9:
continue
print(index)
else:
print("循环正常结束后,才会执行的代码")
print("循环后的代码")
结果:
layer =int(input("请输入要打印的层数"))
index = 0
while index < layer:
colum_nums=0
while colum_nums <= index:
print("*",end="")
colum_nums += 1
print()
index +=1
结果:
layer =int(input("请输入要打印的层数"))
index = 1
while index <layer:
print("*" *index)
index +=1
i = 1
while i<=9:
j =1
while j <=i:
print(f"{j}x{i} ={j*i}",end="\t")
j+=1
print()
i+=1
结果: 不积跬步无以至千里 加油每一个有梦想的人。
|