流程控制
选择结构
if
if 表达式 :
语句
elif 表达式 :
语句
else :
语句
# 序列的空值、空字符串都是假
# python 以缩进判断条件后操作而不是"{ }"
# python 没有switch case语句
# &&、||用and、or表示
# 想要少写分支条件,用嵌套
不多说
if嵌套
if 表达式1:
语句
if 表达式2:
语句
elif 表达式3:
语句
else:
语句
elif 表达式4:
语句
else:
语句
练习:
- 判断某年某月某日是这一年的第几天
- 思路:先判断是否闰年、月份和日期合法性,之后利用循环对每月所有的天数累加;还可以使用序列(元组)存储一个每月对应的天数,累加时用序列中的值即可
while True:
year = int(0)
month = int(0)
day = int(0)
leapyear = False
passedtotalday = int(0)
totalday = int(0)
i = int(1)
# input year
year = int(input("year:"))
if (year % 4 == 0 and year % 100 != 0) or year % 400 == 0:
leapyear = True
else:
leapyear=False
# input month and check
month = int(input("month:"))
while month<=0 or month>12:
# month should not be -1 or 13
month = int(input("Wrong!input again.month:"))
else:
# passed
month=month
day = int(input("day:"))
while day < 1 or day > 31:
# check day is legal
day = int(input("Wrong!input again.day:"))
if month == 1 or month == 3 or month == 5 or month == 7 or month == 8 or month == 10 or month == 12:
day = day
elif month == 2:
# check leap month
while (leapyear == False and day > 28) or day > 29:
day = int(input("Wrong!input again.day:"))
else:
day = day
else:
#check day of 30days
while day > 30:
day = int(input("Wrong!input again.day:"))
else:
day = day
# print Y-M-M-isleap?
print(str(year)+'-'+str(month)+'-'+str(day)+'-'+str(leapyear))
# count sum days,
while i<=month:
if i-1==1 or i-1==3 or i-1==5 or i-1==7 or i-1==8 or i-1==10 or i-1==12 :
# add days when one month passed
i+=1
passedtotalday+=31
elif i-1 == 2:
i += 1
# Feb passed
if leapyear == True:
passedtotalday += 29
elif leapyear == False:
passedtotalday += 28
elif i-1==4 or i-1==6 or i-1==9 or i-1==11:
# add days when one month passed
i += 1
passedtotalday += 30
else:
# days do nothing
i += 1
totalday = passedtotalday + day
print("total days:"+str(totalday))
循环结构
Python中,while、for都可以有else,但循环被break时,不执行else里面的内容
while/while else
while 判断条件(condition):
执行语句(statements)……
练习:
- 1+…+100
- 不用思路
i=1
sumi=0
while i<=100:
sumi+=i
i+=1
print(sumi)
for
for循环通常用于迭代表达式的遍历循环
迭代对象可以是:序列、字符串、迭代器对象、生成器对象、文件对象
迭代器对象:range(),参考,用来生成指定范围的数,可以被转换为列表等
range()的结束位置也同序列的原理
x in range(i,j,k)就相当于( x = i ; x < j ; x + = k ),可以缺省起始和步长,与序列一样
二维的情况下,x可被当作迭代对象,y in x
for <variable> in <sequence>:
<statements>
else:
<statements>
练习:输出一个矩阵
# 一次循环就足够了
for x in range(1,5):
print(x,end=" ")
print(x, end=" ")
print(x, end=" ")
print(x)
x+=1
# 这样写有些臃肿
y=0
for x in range(1, 5):
print()
y=0
while y<4:
print(x, end=" ")
y+=1
x += 1
for x in range(1,5):
print(x,x,x,x)
print()
x+=1
break和continue
- break:从判断条件跳出循环
- continue:直接回到下次循环判断(跳过本次循环)
不多说
作业:
- 输⼊⼀个学⽣分数,分数0-100分之间按,90以上A,80-90为B,70-79为C,60-69为D,60分以下 E,输出等级
- 思路:不需要
# 按分数分等级
while True:
score=int(input("分数:"))
gread=" "
if score>=60:
if score>=90:
if score>=95:
gread="A+"
else:
gread="A-"
elif score>=80:
if score>=85:
gread="B+"
else:
gread="B-"
elif score>=70:
if score>=75:
gread="C+"
else:
gread="C-"
elif score>=60:
if score>=65:
gread="D+"
else:
gread="D-"
else:
gread="不及格"
print(gread)
- 使⽤while和for循环,计算1-100的和,以及偶数和与奇数和
- 思路:不需要
sumx=0
for x in range(101):
sumx+=x
print("1~100的和:"+str(sumx))
sumx=0
for x in range(1,101,2):
sumx += x
print("1~100的奇数和:" + str(sumx))
sumx=0
for x in range(0,101,2):
sumx += x
print("1~100的偶数和:" + str(sumx))
- 利⽤循环嵌套打印,9×9乘法表
- 思路:小循环的范围要与大循环的值相同
for i in range(1,10):
for j in range(1,i+1):
k=i*j
print(str(i)+" × "+str(j)+" = "+str(k),end=" ")
# 用字符串填充来对齐
# print("{0} × {1} = {2:2d}".format(y,x,(x*y)),end=" ")
print()
- 打印100-999之间的水仙花数
水仙花数:它的每个位上的数字的 3次幂之和等于它本身(例如:1 ^ 3 + 5 ^ 3+ 3 ^ 3 = 153)
- 思路:用int的特性和%来分离每一位,也可以用整除//
print("水仙花数:")
for x in range(100,1000):
x1 = int(x / 100)
x2 = int((x % 100) / 10)
x3 = int((x % 100) % 10)
if (x1 ** 3 + x2 ** 3 + x3 ** 3 == x):
print(x)
- 扩展:输入一个数判断他是不是水仙花数
while True:
num = int(input("100~999的数:"))
while num>=100 and num<1000:
b1=int(num/100)
b2=int((num%100)/10)
b3=int((num%100)%10)
print(b1,b2,b3)
if(b1**3+b2**3+b3**3 == num):
print(str(num)+"是一个水仙花数")
break
else:
print(str(num) + "不是一个水仙花数")
break
else:
continue