2017-06-30 12 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() 

とエラーが デフreveal_occupantsの本体です。 :このエラーは克服し、別のアプローチを提案してくださいすることができますか

があまりにも

+0

スクリーンショットとしてではなく、テキスト情報(コード、エラー)_as text_を投稿してください。 – marcelm

答えて

1

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

あなたoccupy_huts()機能は、(それがグローバル変数hutsを移入するが、それを返さない)何も返さないので、huts = occupy_huts()声明hutsは今None(デフォルトの関数である後明示的に何かを返さない場合の戻り値)。

reveal_occcupants(index,huts,dotted_line) 

ソリューションは単純です:次に、あなたは(今Nonereveal_occupants()からhuts変数にこれを渡し、それは動作しますが、代わりに(ほとんど常に非常に悪い考えである)グローバルに取り組んでNoneを返すので、occupy_hutsを変更しますローカル変数にし、それを返します:

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

我々はそれに取り組んでいる一方で、あなたはあなたが、(呼び出された場合、この変数が作成された前occupy_huts()が壊れる)脆性である、あまりにもoccupantsためglobalを使用していますできたただ、引数として渡す:run()

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

、その後を:

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()関数はどちらか何も返さないからです。ローカル変数indexを返すように変更する必要があります。

0

LEN()メソッドは、パラメータとしてオブジェクトを受け入れ、「はTypeError型 『NoneType』のオブジェクトはLEN()を持っていません」 。あなたのケースでは 、43行目では小屋がありませんので、エラーが発生する可能性があります。

条件は以下のような行42

if huts is None: 
    return 
+0

これは症状をマスクしますが、実際の問題を治しません。 –

0

後に私の推測では呼ばれませんでしたoccupy_huts()ので、「小屋」は、なし型であるということであるならば、あなたは挿入する必要があります。または、 "huts"変数のスコープに問題があります。これは、occupy_huts()関数の外部で空のセットとして宣言することでクリアされる可能性があります。

また、Pythonの構文を利用して43行目を "for hut in huts:"に変更することもできます。小屋のインデックスも必要な場合は、「小屋のために列挙してください(小屋)」を試してください。

+0

'occupy_huts()'が呼び出されたとき、 'run()'関数をチェックしてください。 –

0

あなたのメソッド「reveal_occupants」は小屋として空の値を受け取ります。つまり、そのタイプの小屋はなしです。なぜこの値のlenを得ることができないのですか。ここで

関連する問題