第三週作業 Chapter 5課後練習

2021-08-17 04:26:23 字數 2872 閱讀 9289

5-2 更多的條件測試

**:# test1

str1 = "abc"

print(str1 == "abc")

# test2

str2 = "abc"

print(str2.lower() == "abc")

# test3

print(1.5 == 1.5)

print(1.5 != 1.5)

print(1.5 >= 1.5)

print(3 > 2)

print(3 < 2)

print(1 <= 2)

# test4

age1 = 23

age2 = 21

print(age1>=23 and age2>=22)

print(age1>22 or age2>22)

# test5

names = ['mike','joey','peter']

print('peter' in names)

print('peter' not in names)

執行結果:

5-5 外星人顏色#3

**:#test1

alien_color = 'red'

if alien_color == 'green':

print("you just earned 5 points!")

elif alien_color == 'yellow':

print("you just earned 10 points!")

else:

print("you just earned 15 points!")

#test2

alien_color = 'yellow'

if alien_color == 'green':

print("you just earned 5 points!")

elif alien_color == 'yellow':

print("you just earned 10 points!")

else:

print("you just earned 15 points!")

#test3

alien_color = 'green'

if alien_color == 'green':

print("you just earned 5 points!")

elif alien_color == 'yellow':

print("you just earned 10 points!")

else:

print("you just earned 15 points!")

執行結果:

5-7 喜歡的水果

**:執行結果:

5-9 處理沒有使用者的情形

**:usernames = ['admin','mike','eric','joey','sid']

if usernames:

for username in usernames:

if username == 'admin':

print("hello admin, would you like to see a status report?")

else:

print("hello " + username + ", thank you for logging in again.")

else:

print("we need to find some users!")

usernames =

if usernames:

for username in usernames:

if username == 'admin':

print("hello admin, would you like to see a status report?")

else:

print("hello " + username + ", thank you for logging in again.")

else:

print("we need to find some users!")

執行結果:

5-11 序數

**:numbers = [1,2,3,4,5,6,7,8,9]

for number in numbers:

if number == 1:

print(str(number)+"st")

elif number == 2:

print(str(number)+"nd")

elif number == 3:

print(str(number)+"rd")

else:

print(str(number)+"th")

執行結果:

第三週 課後實踐 閱讀程式

問題及 檔名稱 project.cpp 作 者 chenqin.完成日期 2015年3月21日 版 本 號 v1.0 問題描述 按照封裝與資訊隱藏的原則,除非特別需要,類中的資料成員需要設定為私有。由此帶來的問題是,在類外如何訪問這些私有成員?下面4段程式概括了常用的方法。請仔細閱讀下面的程式,在閱...

第三週 課後實踐 閱讀程式

按照封裝與資訊隱藏的原則,除非特別需要,類中的資料成員需要設定為私有。由此帶來的問題是,在類外如何訪問這些私有成員?下面4段程式概括了常用的方法。請仔細閱讀下面的程式,在閱讀過程中,畫出物件 變數在記憶體中的表示圖,寫出這些程式的執行結果 包括變數的變化過程及程式的最終輸出 達到徹底理解這些機制的目...

計算機導論第三週課後總結

1 一條無限長的紙帶tape 2 乙個可以讀寫頭head 3 乙個控制器 包括一套控制規則集合和乙個狀態暫存器 其中q是又窮個狀態的集合 是字母表,即符號的集合 s q是初始狀態 h q是停機狀態的集合,當控制器內部狀態為停機狀態時圖靈機結束計算 是轉移函式,即控制器的規則集合。控制器的規則格式為 ...