|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
分支和循环2
1按照100分制,90分以上为A,80到90为B, 60到80为C, 60一下为D, 写一个程序,当用户输入分数,自动转换为ABCD的形式打印
#method 1
temp=input('Please give your grades')
Score=int(temp)
if 90 < Score <=100:
print('A')
else:
if 80 <Score <=90 :
print('B')
else:
if 60 <= Score <=80 :
print('C')
else:
print("you've failed the exam")
#method 2
temp=input('Please give your grades')
Score=int(temp)
if 90 < Score <=100:
print('A')
elif 80 <Score <=90 :
print('B')
elif 60 <= Score <=80 :
print('C')
else:
print("you've failed the exam")
2 Python可以有效避免悬挂else
什么叫悬挂else
C语言中if 和else就近匹配
本来目的是与第一个if配而程序跑起来后与第二个if 匹配
而Python强制使用缩进,否则程序出错。
3 条件表达式(三元操作符)几元代表几个操作数
语法:x if 条件 else y
条件true 执行x, false 执行y
4 断言
assert
当这个关键字后边的条件为假的时候程序自动崩溃并抛出Assertionerror的异常
Bsp: assert 3>4
可以用它在程序中置入检查点,当需要确保程序中的某个条件一定为真才能让程序正常工作的话,
assert is then very useful
|
评分
-
查看全部评分
|