python第三课循环-1:
1.while循环条件 while后面写要循环的条件
示例1:输出1-100的偶数和
注意对齐格式
shu=0;
num=0;
while shu<=100 :
num=num+shu;
shu=shu+2;
print(num);
示例2:请用户输入数字,求和,直到输出0退出
num=0;
while True:
a=int(input("输入一个数:"));
if a==0:#输入0时结束循环
break;
num=num+a;#计算之前输入过a的值
# a=a+1;#用来记录a下一步的输入,有没有无所谓
print(num);
while … else 在条件语句为 false 时执行 else 的语句块
例:count = 0
while count < 5:
print (count, " 小于 5")
count = count + 1
else:
print (count, " 大于或等于 5")
2.range()函数
如果你需要遍历数字序列,可以使用内置range()函数。它会生成数列,
range(5): 一个参数 从0开始 到5不包含自己
range(5,9):二个参数 从5开始 到9不包含自己
range(5