python语言基础-笔记
计算机的流程控制:顺序结构、选择结构、循环结构
1.选择结构:1) 单分支结构 if 语句
1 #练习:取钱! 2 money=1000 3 s=int(input('请输入一个整数:')) 4 if s<=money: 5 money=money-s 6 print('您已取款成功,余额为',money)
2)双分支结构 if else 语句
#练习:录入一个整数,判断这个数是奇数还是偶数 a=int(input('请输入一个整数:')) if a%2==0: print(a,'为偶数') else: print(a,'为奇数')
3)多分支结构 if elif elif... else
#练习:输入一个整数,90-100为A,80-89为B以此类推,0-60为E,小于0或大于100非法 score=int(input('请输入您的成绩:')) print(score) if score>=90 and score<=100: print('您的等级为A。') elif score>=80 and score<=89: print('您的等级为B。') elif score>=70 and score<=79: print('您的等级为C。') elif score>=60 and score<=69: print('您的等级为D。') elif score>=0 and score<=59: print('您的等级为E。') else: print('您输入的成绩有误!')4.
4)嵌套结构 if if else else if else
'''会员 消费金额>=200 0.8 >=100 0.9 不打折 非会员 >=200 0.95 不打折''' a=input('您是否为会员?是/否') m=int(input('您的消费金额为:')) if a=='是': if m<=200: print('打九折,实际付款金额为:',0.9*m) elif m<=100: print('不打折,实际付款金额为:',m) else: print('打八折,实际付款金额为:',0.8*m) else: if m>=200: print('打九五折,实际付款金额为:',0.95*m) else: print('不打折,实际付款金额为:',m)
5)条件表达式(双分支结构的简写)
语法结构:if 判断条件 else y
运算规则:对象布尔值为Ture,则返回x;对象布尔值为False,则返回y
6)pass语句(什么都不做,只是一个占位符,用在语法上需要语句的地方)
when:先搭建语法结构,还没想好代码怎么写的时候
what:{if语句的条件执行体
for...in语句的循环体
定义函数时的函数体}
1 #例: 2 a=input('有会员卡吗?y/n') 3 if a=='y': 4 pass 5 else: 6 pass#系统不报错
浙公网安备 33010602011771号