Python开发[类型错误]

时间:2017-06-30 11:10:51

标签: python macvim

我是初学者,最近开始进行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 。 &#34; TypeError:类型对象&#39; NoneType&#39;没有len() &#34;

如何克服这个错误,请建议另一种方法

4 个答案:

答案 0 :(得分:1)

这里:

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

您的occupy_huts()函数没有返回任何内容(它填充全局变量huts但不返回它),因此huts = occupy_huts()语句{{ 1}}现在是huts(默认函数返回值,如果你没有明确地返回一些东西)。然后,您将此(NoneNone变量传递给huts

reveal_occupants()

解决方案很简单:修改 reveal_occcupants(index,huts,dotted_line) 所以不是在处理全局(这几乎总是一个非常糟糕的主意)并返回occupy_huts,而是在局部变量上工作并返回它: / p>

None

虽然我们正在使用它,但您def occupy_huts(): huts = [] while len(huts) < 5: random_choice = random.choice(occupants) huts.append(random_choice) return huts 也使用global,这很脆弱(如果在创建此变量之前调用occupants将会中断),虽然你可以把它作为参数传递:

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存在类似问题:

process_user_choice()

因为您的while keep_playing == 'y': huts = occupy_huts() index = process_user_choice() 函数也没有返回任何内容。您应该修改它,以便返回其本地变量process_user_choice()

答案 1 :(得分:0)

len()方法接受一个对象作为参数。 在你的情况下,在第43行,小屋可能是无,所以你可能会收到错误。

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

if huts is None:
    return

答案 2 :(得分:0)

我的猜测是“huts”是None-type,因为从未调用了occupy_huts()。或者“huts”变量的范围存在问题 - 可以通过将其声明为occupy_huts()函数之外的空集来清除它。

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

答案 3 :(得分:0)

你的方法&#34; reveal_occupants&#34;作为小屋获得空值。这意味着,那种类型的小屋是无。这就是为什么你无法获得这个价值的原因。