(五)流程控制if...else

既然我们编程的目的是为了控制计算机能够像人脑一样工作,那么人脑能做什么,就需要程序中有相应的机制去模拟。人脑无非是数学运算和逻辑运算,对于数学运算在上一节我们已经说过了。对于逻辑运算,即人根据外部条件的变化而做出不同的反映,比如

1 如果:女人的年龄>30岁,那么:叫阿姨
注意缩进
age_of=4
int(age_of),
if age_of>30:
    print('hello aunt')

else: print('hello miss')
age_of=int(input('input age')) #强制转换为整型
print(type(age_of))
if age_of > 30:
    print('hello aunt')

else: print('hello miss')
if 嵌套
score=int(input('pls input your score'))
if score>=90 :
    print('youxiu')
elif score>80:
    print('lianghao')
elif score>70:
        print('putong')

else: print('hencha')

  if 条件1:

    缩进的代码块

  elif 条件2:

    缩进的代码块

  elif 条件3:

    缩进的代码块

  ......

  else:  

    缩进的代码块
    
    
    
  today=input('>>>:') if today in ['saturaday','sunday']: print('lang') elif today in ['Monday','Tuesday','Wednessday','Thursday','Friday']: print('on duty') else: print('''必须输入其中一种: Monday Tuesday Wednessday Thursday Friday ''' )

猜你喜欢

转载自www.cnblogs.com/morron/p/8858922.html