Python-程序流程控制
程序流程控制
分支语句
-
if
score = int(input("请输入您的成绩: ")) if score >= 90: print("您的等级为A. ") if (score >= 78) and (score < 90): print("您的等级为B. ") x, y = 7, 8 bigger = x if x>y else y # bigger = 8
-
if-else
score = int(input("请输入您的成绩: ")) if score >= 60: if score >= 85: print("优秀. ") else: print("及格. ") else: print("不及格! ")
-
if-elif-else
score = int(input("请输入您的成绩: ")) if score >= 90: grade = 'A' elif score >= 78: grade = 'B' elif score >= 68: grade = 'C' elif score >= 60: grade = 'D' else: grade = 'F' print("Grade = " + grade)
循环语句
-
while
i = 0 k = 0 while k <= 1000: print("i = " + str(i)) print("i^2 = " + str(k)) print("-------") # if k == 625: # break i += 1 k = i ** 2 else: print("While Over! ")
-
for
print("----字符串----") for i in "zifuchuan": print(i, end="") else: print("\nFor Over!") print("----列 表----") number = [1, 2, 3, 4, 5] for i in number: if i == 3: continue print(i) else: print("For Over!")
跳转语句
break
continue
return