python.day.05——常用控制流类型if&for&while

一、条件判断句:

  • if....elif....else....back
  • if 后面是布尔值
  • 冒号控制父级  子级
  • Python里面是用缩进/空格来决定父级  子级
  • Python里面代码执行顺序是从上往下执行
score=77
if score>=60:
   print("及格")
elif score<60:    #elif后面必须跟条件
   print("不及格")
else:            #else后面不能跟条件
  • input():控制台输入默认为string
data = input("请输入一个数据:")
print(data)
  • 练习:
    • #现在有一个考试系统 会根据你输入的分数判断 学生的等级
    • #强制规定只能输入数字
    • #小于0分或者大于100分 那么就是数据无效
    • #0~60不及格 60<=x<80及格 80<=x<90 良好 90<=x<100 优秀!
score = int(input("请输入您的分数:"))
if score <0 or score >100:
   print("成绩无效")
elif score < 60 and score >= 0:
   print("不及格")
elif score < 80 and score >= 60:
   print("良好")
else:
    print("优秀")
二、循环
  • while.....for...
while a ==1:    #跟条件
   print("a==1")

a = 0
 while True:
         print("a的值%s" %a)
         a -=1
         if a>0:
              continue #继续循环
         elif a <= 0:
              break #跳出循环

三、遍历 for ...in...

猜你喜欢

转载自www.cnblogs.com/Rosay9204781/p/9160313.html