小甲鱼Python第七讲课后习题

0.if not(money < 100):上边这行代码相当于?

if money>=100

 

1.assert 的作用是什么?

assert “断言”,当这个关键字后边的条件为假的时候,程序自动崩溃并抛出 AssertionReeor

 

2.假设有x=1,y=2,z=3,请问如何快速将三个变量的值相互交换?

x,y,z=1,2,3

x,y,z=z,y,x

print(x,y,z)

 

3.你听说过成员资格运算符吗?

Python 有一个成员资格运算符:in,用于检查一个值是否在序列中,如果在序列中返回True,否则返回False
>>> name ="小甲鱼"
>>> '鱼' in name
True
>>> '哈哈' in name
False

 

动手

0.按照100分制,90分以上成绩为A,80到90为B,70到80为C,60到70为D,其余为不及格

注意:三元操作符   small = x if x < y else y

自己写的代码还是很Low

while True:
temp=input("chengji")
while temp.isspace() or not temp.isdigit():
temp =input("chongxinshuruchengji")
a=int(temp)
if a>=90:
print('A')
elif a>=80 and a<90:
print('B')
elif a>=70 and a<80:
print('C')
elif a>=60 and a<70:
print('D')
else:
print('不及格')
附加:
temp = input("请输入分数:")
score = int(temp)
while score > 100 or score <0:
    temp = input("输入错误,请重新输入")
    score = int(temp)
if 90 <= score <= 100:
    print("A")
elif 80 <= score < 90:
    print("B")
elif 70 <= score < 80:
    print("C")
elif 60 <= score <70:
    print("D")
else:
    print("不及格")

1.转换成三元操作符

x,y,z =6,5,4

if x<y:

  small=x

  if z<small:

    small=z

elif y<z:

  small=y

else:

  small=z

 

small = x if x<( y if y<z else z) else ( y if y<z else z)

posted @ 2018-05-07 16:02  小红帽爱吃大灰狼  阅读(4227)  评论(0编辑  收藏  举报