2017-06-30 78 views
-5

我是一個初學者,最近開始python開發。 代碼我工作:Python開發[類型錯誤]

import random 
import textwrap 

def show_message(dotted_line,width): 

    print(dotted_line) 
    print("\033[1m"+ "Attack of clones:" + "\033[0m") 

    message = (
    "The war between humans and their arch enemies , Clones was in the offing. Obi-Wan, one of the brave Jedi on his way ," 
    "he spotted a small isolted settlement .Tired and hoping to replenish his food stock , he decided to take a detour." 
    "As he approached the village, he saw five residence , there was no one to be seen around.He decided to enter") 
    print(textwrap.fill(message, width = width)) 

def show_mission(dotted_line): 
    print("\033[1m"+ "Mission:" + "\033[0m") 
    print('\t Choose the hit where Obi wan can rest...') 
    print("\033[1m"+ "TIP:" + "\033[0m") 
    print("Be careful as there are Stormtroopers lurking around!") 
    print(dotted_line) 


def occupy_huts(): 
    global huts 
    huts = [] 

    while len(huts) < 5: 
     random_choice = random.choice(occupants) 
     huts.append(random_choice) 



def process_user_choice(): 
    message = "\033[1m"+ "Choose the hut to enter (1-5) " + "\033[0m" 
    uc = input("\n" + message) 
    index = int(uc) 
    print("Revealing the occupants...") 
    message = "" 



def reveal_occcupants(index,huts,dotted_line): 
    for i in range (len(huts)): 
     occupant_info = "<%d:%s>"%(i+1,huts[i]) 
     if i + 1 == index: 

      occipant_info = "\033[1m"+ "" + "\033[0m" 
     message += occupant_info + " " 
    print("\t" + message) 
    print(dotted_line) 



def enter_huts(index,huts,dotted_line): 
    print("\033[1m"+ "Entering Hut %d ..." %index + "\033[0m") 

    if huts[index - 1] == 'clones': 
     print("\033[1m"+ "There's Stormtrooper Here!!" + "\033[0m") 
    else: 
     print("\033[1m"+ "It's Safe here!" + "\033[0m") 
    print(dotted_line) 



def run(): 
    keep_playing = 'y' 
    global occupants 
    occupants = ['clones','friend','Jedi Hideout'] 
    width = 70 
    dotted_line = '-' * width 

    show_message(dotted_line, width) 
    show_mission(dotted_line) 

    while keep_playing == 'y': 
     huts = occupy_huts() 
     index = process_user_choice() 
     reveal_occcupants(index,huts,dotted_line) 
     enter_huts(index,huts,dotted_line) 
     keep_playing = raw_input("Play Again?(y/n)") 

if __name__ == '__main__': 
    run() 

和誤差是在 DEF reveal_occupants體。 「類型錯誤:類型‘NoneType’的對象沒有LEN()

此錯誤如何可以克服的,並請建議的替代方法也

+0

請發佈文本信息(代碼,錯誤)_文字_,而不是截圖。 – marcelm

回答

1

這裏:

while keep_playing == 'y': 
    huts = occupy_huts() 

occupy_huts()功能不返回任何東西(它填充一個全局變量huts但不會返回),所以在huts = occupy_huts()語句之後huts現在None(默認功能返回值,如果你不明確地返回一些東西)。然後你通過這個(現在Nonehuts變量reveal_occupants()

reveal_occcupants(index,huts,dotted_line) 

解決方法很簡單:修改occupy_huts因此,與其在全球的工作(這是幾乎總是一個非常糟糕的主意),並返回None,它的工作原理在一個局部變量,並返回它:

def occupy_huts(): 
    huts = [] 
    while len(huts) < 5: 
     random_choice = random.choice(occupants) 
     huts.append(random_choice) 
    return huts 

雖然我們在這,您使用的是globaloccupants過,這是脆(occupy_huts()將打破,如果叫已經建立的這個變數之前),而你可以只是把它作爲參數:

def occupy_huts(occupants): 
    huts = [] 
    while len(huts) < 5: 
     random_choice = random.choice(occupants) 
     huts.append(random_choice) 
    return huts 

,然後在run()

def run(): 
    keep_playing = 'y' 
    occupants = ['clones','friend','Jedi Hideout'] 
    # ... 
    while keep_playing == 'y': 
     huts = occupy_huts(occupants) 

這裏有趣的事情是,你傳遞參數的世俗的東西是多是常量和對程序的邏輯沒有影響(即dotted_lines),但是對於重要的事情使用全局變量 - 應該是相反的方式(在模塊的開頭聲明dotted_lines爲pseudo_constant,並且不用費力地將它傳遞給函數);)

另外,note那你有process_user_choice()這裏一個類似的問題:因爲你的process_user_choice()功能

while keep_playing == 'y': 
    huts = occupy_huts() 
    index = process_user_choice() 

不返回任何東西。您應該修改它,以便返回其本地變量index

0

LEN()方法接受一個對象作爲參數。 在你的情況下,在43行,小屋可能是None,所以你可以得到一個錯誤。

你應該插入如果像線以下後,42

if huts is None: 
    return 
+0

這將掩蓋症狀,但不會治癒真正的問題。 –

0

我的猜測條件是「小屋」是無型,因爲occupy_huts()從未被調用。或者,「huts」變量的範圍存在問題 - 可以通過將其聲明爲一個空的set而設置在occupy_huts()函數之外。

另外,您可以利用Python的語法,並將第43行更改爲「for hut in huts:」。如果您還需要小屋的索引,請嘗試「對於小屋,i-hut在枚舉(小屋):」。

+0

'occupy_huts()'被調用,檢查'run()'函數。 –

0

你方法「reveal_occupants」接收空值作爲小屋。這意味着,這種類型的小屋是無。那就是爲什麼你不能得到這個價值的長處。