笨辦法學python加分習題38

2021-08-15 12:55:28 字數 1612 閱讀 5763

python版本:3      若有錯誤,

敬請指出 模組名稱:測試.py

#加分習題38

states =

cities =

cities['ny'] = 'new york'

cities['or'] = 'portland'

print('-'*10,'1')

print("ny stae has:",cities['ny'])

print("or state has:",cities['or'])

print('-'*10,'2')

print("michigan's abbreviation is:",states['michigan'])

print("florida's abbreviation is:",states['florida'])

print('-'*10,'3')

print("michigan has:",cities[states['michigan']])

print("florida has:",cities[states['florida']])

print('-'*10,'4')

for state,abbrev in states.items():

print("%s is abbreviated %s"% (state,abbrev))

print('-'*10,'5')

for abbrev,city in cities.items():

print("%s has the city %s"% (abbrev,city))

print('-'*10,'6')

for state,abbrev in states.items():

print("%s state is abbreviated %s and has city %s"%(state,abbrev,cities[abbrev]))

print('-'*10,'7')

state = states.get('texas',none)

if not state:

print("sorry,no texas")

city = cities.get('tx','does not exist')

print("the city for the state 'tx' is:%s"% city)

print('-'*10,'8')

state = states.get('california',none)

if not state:

print("sorry,no california")

else:

print("да")

#補充:python 字典 get() 函式返回指定鍵的值,如果值不在字典中返回預設值。

#dict.get(key, default=none)

#key -- 字典中要查詢的鍵。

#default -- 如果指定鍵的值不存在時,返回該預設值值。

#題目錯了

執行截圖:

笨辦法學python加分習題30

python版本 3 若有錯誤,敬請指出 模組名稱 測試.py 加分習題30 people 30 cars 40 buses 15 if cars people print we should take the cars.elif cars people print we should not ta...

笨辦法學python加分習題36

python版本 3 若有錯誤,敬請指出 模組名稱 測試.py 我可能是個傻姑娘.加分習題36 def a print 吃西紅柿炒蛋時,你常常煩惱是該先吃西紅柿還是先雞蛋嗎?print a 是 nb 否 answer input if answer.upper a b elif answer.upp...

笨辦法學Python習題35 加分習題

加分習題 1.把這個遊戲的地圖畫出來,把自己的路線也畫出來。2.改正你所有的錯誤,包括拼寫錯誤。3.為你不懂的函式寫註解。記得文件註解該怎麼寫嗎?4.為遊戲新增更多元素。通過怎樣的方式可以簡化並且擴充套件遊戲的功能呢?5.這個 gold room 遊戲使用了奇怪的方式讓你鍵入乙個數字。這種方式會導致...