2017-06-30 53 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”接收空值作为小屋。这意味着,这种类型的小屋是无。那就是为什么你不能得到这个价值的长处。