《笨辦法學python》加分習題39 我的答案

2021-08-08 02:27:39 字數 2454 閱讀 8972

這是我自己學習的答案,會盡力寫的比較好。還望大家能夠提出我的不足和錯誤,謝謝!

print

"wait there's not 10 things in that list, let's fix that."

stuff = ten_things.split(' ')

more_stuff = ["day", "night", "song", "frisbee", "corn", "banana", "girl", "boy"]

while len(stuff) != 10:

next_one = more_stuff.pop()

print

"adding: ", next_one

print

"there's %d items now." % len(stuff)

print

"there we go: ", stuff

print

"let's do some things with stuff."

print stuff[1]

print stuff[-1] # whoa! fancy

print stuff.pop()

print

' '.join(stuff) # what? cool!

print

'#'.join(stuff[3:5]) # super stellar!

1、

stuff = ten_things.split(' ') # split('', ten_things)

next_one = more_stuff.pop() # pop(more_stuff)

print stuff.pop() # pop(stuff)

print

' '.join(stuff) # what? cool! join(stuff, ' ')

print

'#'.join(stuff[3:5]) # super stellar! join(stuff[3:5], '#')

2、

# 用『 』拆開ten_things;為『 』和ten_things呼叫了split函式

stuff = ten_things.split(' ') # split('', ten_things)

# 提取出more_stuff中-1位元素並列印出來;為more_stuff[-1]位呼叫pop函式

next_one = more_stuff.pop() # pop(-1,more_stuff)

# 類似如上pop()

print stuff.pop() # pop(-1,stuff)

#如習題答案

print

' '.join(stuff) # what? cool! join(stuff, ' ')

#類似習題答案

print

'#'.join(stuff[3:5]) # super stellar! join(stuff[3:5], '#')

4、類

5、dir(something)中,dir()在pydoc的解釋如下:

help on built-in function dir in modulebuiltin:

dir(…)

dir([object]) -> list of strings

if called without an argument, return the names in the current scope.

else, return an alphabetized list of names comprising (some of) the attributes

of the given object, and of attributes reachable from it.

if the object supplies a method named __dir__, it will be used; otherwise

the default dir() logic is used and returns:

for a module object: the module's attributes.

for a class object: its attributes, and recursively the attributes

of its bases.

for any other object: its attributes, its class's attributes, and

recursively the attributes of its class's base classes.

看了下,大概是如果something有定義dir這個方法的話就呼叫這個方法,如果沒有就返回something所在類的其他方法名稱。

笨辦法學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加分習題38

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 c...