代码之家  ›  专栏  ›  技术社区  ›  Desmond

Python开发[类型错误]

  •  -5
  • Desmond  · 技术社区  · 8 年前

    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 REVEL_乘员 . TypeError:“NoneType”类型的对象没有len() "

    如何克服这一错误,请提出替代方法

    4 回复  |  直到 8 年前
        1
  •  1
  •   bruno desthuilliers    8 年前

    在这里:

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

    occupy_huts() 函数不返回任何内容(它填充全局变量 huts 但是没有返回),因此 huts = occupy_huts() 陈述 小屋 None 小屋 变量至 reveal_occupants() :

        reveal_occcupants(index,huts,dotted_line)
    

    解决方案很简单:修改 occupy_huts 因此,与其在全球范围内工作(这几乎总是一个非常糟糕的主意)并返回 ,它作用于局部变量并返回:

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

    global 对于 occupants 太脆了( 占领小屋() 如果在创建此变量之前调用,则将中断),而您可以将其作为参数传递:

    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 ),但在重要的事情上使用全局变量-实际上应该是相反的(在模块的开头将虚线声明为伪常量,不要麻烦将其传递给函数);)

    process_user_choice()

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

    自从你 process\u user\u choice() index .

        2
  •  0
  •   Hasan Güçlü    8 年前

    在您的例子中,在第43行,huts可能没有,因此您可能会得到一个错误。

    您应该在第42行后插入如下条件

    if huts is None:
        return
    
        3
  •  0
  •   AlexM    8 年前

    我的猜测是“huts”不是类型,因为从来没有调用过occupt\u huts()。或者“huts”变量的范围有问题——这可以通过在occue\u huts()函数外将其声明为空集来解决。

    此外,您可以利用Python的语法,将第43行更改为“for hut in huts:”。如果您还需要小屋的索引,请尝试“for hut,i-hut in enumerate(huts):”。

        4
  •  0
  •   Andrew    8 年前

    您的方法“reveal_Occupers”作为小屋接收空值。这意味着,那种类型的小屋是没有的。这就是为什么你不能得到这个值。