小甲魚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)


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM