2016-04-03 27 views
0

イム新しい(-ish)に関数内の変数を編集し、私は私が終わった後、私は大きな間違い製のだ実現し、今日のゲームを作った方法:私が持っていた関数の内部アクセスとPythonへのpython

を変数にアクセスして編集し、その他のファンクションでアクセスしたり変更したりした変数を変更することもできます。私はそれをどうやって行うのか分かりません。

私は長い間研究してきましたが、問題を解決する可能性のあるものはほとんど見つかりませんでしたが、いくつか試しましたが、うまくいかず、他の方法を理解できません。

あなたは問題で私を助けてみてください、あなたが他の人を見つけたなら、デバッグにあまりにも良い、私はイムないよう教えてくださいでした:(

ここでは以下のコード、(私が置かれているそのかなり大きいです私は大胆にアクセスし、変更する必要のある変数): ランダム輸入randint プリント( "ゴーストゲームV2.0") プリント( "難易度を選択する")

score  = 0 
alive  = True 
difficulty = 0 
doors  = 0 
ghost_door = 0 
action  = 0 
ghost_power = 0 
       #define the function 'ask_difficulty' 
def ask_difficulty() : 
    difficulty = input ("Hard, Normal, Easy") 
    set_difficulty() 



       # define the function 'set_difficulty' which sets the   difficulty. 
def set_difficulty() : 
if difficulty == 'Hard' or 'Normal' or 'Easy' : 
    if difficulty == 'Hard' : 
     doors = 2 

    elif difficulty == 'Normal' : 
     doors = 3 

    elif difficulty == 'Easy' : 
     doors = 5 

else: 
    print ("Invalid input, please type Hard, Normal, or Easy") 
    ask_difficulty() 



      # define the function 'ghost_door_choose' which sets the ghost door and the chosen door 


def ghost_door_choose(x): 
    ghost_door = randint (1, x) 

    print (doors + " doors ahead...") 
    print ("A ghost behind one.") 
    print ("Which do you open?") 

    if doors == 2 : 
     door = int("Door number 1, or door number 2...") 
     if 1 or 2 in door : 
      ghost_or_no() 

     else : 
      print ("Invalid input") 
      ghost_door_choose(difficulty) 


    elif doors == 3 : 
     door = int("Door number 1, door number 2, or door number 3") 
     if 1 or 2 or 3 in door : 
      ghost_or_no() 

     else: 
      print ("Invalid input") 
      ghost_door_choose(difficulty) 


    elif doors == 5 : 
     print("Door number 1, door number 2, door number 3, door number 4,  or door number 5.") 
     if 1 or 2 or 3 or 4 or 5 in door : 
      ghost_or_no() 

     else: 
      print ("Invalid input") 
      ghost_door_choose(difficulty) 


       # define the function 'ghost_or_no' 
def ghost_or_no() : 
    if door == ghost_door: 
     print ("GHOST!!") 
     print ("Initiating battle...") 
     battle() 

    else: 
     print ("No ghost, you\'ve been lucky, but will luck remain with you...") 
     score = score + 1 
     ghost_door_choose(difficulty) 

       # define the function 'battle' which is the battle program 
def battle() : 
    ghost_power = randint (1, 4)    # 1 = Speed, 2 = Strength, 3 = The ghost is not friendly, 4 = The ghost is friendly 

    print ("You have 3 options") 
    print ("You can flee, but beware, the ghost may be fast (flee),") 
    print ("You can battle it, but beware, the ghost might be strong (fight),") 
    print ("Or you can aproach the ghost and be friendly, but beware, the ghost may not be friendly (aproach)...") 
    action = input ("What do you choose?") 

    if flee in action : 
     action = 1 

    elif fight in action : 
     action = 2 

    elif aproach in action : 
     action = 3 

    else : 
     print ("Invalid input") 
     battle() 

    if ghost_power == action : 
     if action == 1: 
      print ("Oh no, the ghost\'s power was speed!") 
      print ("DEFEAT") 
      print ("You\'r score is " + score) 
      alive = False 

     elif action == 2: 
      print ("Oh no, the ghost\'s power was strength!") 
      print ("DEFEAT") 
      print ("You\'r score is " + score) 
      alive = False 

     elif action == 3: 
      print ("Oh no, the ghost wasn\'t friendly ") 
      alive = False 

    elif ghost_power == 4 and action == 3 : 
     print ("Congratulations, The ghost was friendly!") 
     score = score + 1 
     ghost_door_choose(difficulty) 

    elif ghost_power != action and ghost_power != 4 : 
     if action == 1: 
      print ("Congratulations, the ghost wasn\'t fast!") 
      score = score + 1 
      ghost_door_choose(difficulty) 

     elif action == 2: 
      print ("Congratulations, you defeated the ghost!") 
      score = score +1 
      ghost_door_choose(difficulty) 

    elif ghost_power != action and ghost_power == 4 : 
     if action == 1: 
      print ("You ran away from a friendly ghost!") 
      print ("Because you ran away for no reason, your score is now 0") 
      score = 0 
      ghost_door_choose(difficulty) 
     elif action == 1: 
      print ("You killed a friendly ghost!") 
      print ("Your score is now 0 because you killed the friendly ghost") 
      score = 0 
      ghost_door_choose(difficulty) 



        #actual game loop 

ask_difficulty() 

while alive : 
    ghost_door_choose(doors) 
+0

コードが多すぎます... – SteJ

+2

コードが多すぎます。あなたが[mcve]に絞った場合、それは読者を助けるでしょう。 – idjaw

+1

さまざまな関数の変数を共有して更新したい場合は、おそらくクラスが必要です。 –

答えて

0

からは、考えてみましょう:

x=0 
z=22 
def func(x,y): 
    y=22 
    z+=1 
    print x,y,z 

func('x','y')  

あなたはfuncあなたが、私達の機能でエラーを修正するにはUnboundLocalError: local variable 'z' referenced before assignment

を取得します呼び出します。

x=0 
z=22 
def func(x,y): 
    global z 
    y=22 
    z+=1 
    print x,y,z 

globalキーワードを変更するグローバル定義された変数へのローカル参照することができます。

グローバルバージョンではなく、xのローカルバージョンも表示されます。これはあなたが期待するものです。あいまいさは、値のローカルバージョンがない場合です。 Pythonは、globalキーワードを使用しない限り、グローバルに定義された値を読み取り専用として扱います。

コメントに記載されているように、これらの変数を保持するクラスが優れています。

+1

これは私を助けました!ありがとうございます、私はおそらくこれを行うでしょう、あなたが私にクラスが何であるかを説明することができたら、私はそれを感謝します。私はPythonにはかなり新しいです。これを説明する時間をいただきありがとうございます。 – Pixelf

0

スクリプトの先頭にある変数はグローバルであり、それらを関数で設定するには、関数内でグローバルに宣言する必要があります。より小さい例として、

score = 0 
alive = True 

def add_score(value): 
    """Add value to score""" 
    global score 
    score += value 

def kill_kill_kill(): 
    global alive 
    alive = False 

次の手順は複雑になるクラスを作成することです。たとえば、ユーザーごとのスコアをトラッキングするには、ユーザーがそれぞれ独自性を持つ複数の文字を持つことができれば、それらを表すクラスを構築することになります。

0

グローバルキーワードは、あなたが探しているキーワードかもしれません。

たとえば、次のコードにあります。

some_variable = 10 

def some_function(): 
    global some_variable 
    some_variable = 20 

これはsome_variableで、グローバルキーワードを使用せずに(グローバルスコープで)10のままであるように20の値を参照(グローバルスコープで)生じます。

グローバル変数とローカル変数の詳細here

+0

私はあまり良くないが少し簡単なグローバルキーワードで使用されている問題を解決しました。私は助けに感謝し、もはや提案や答えを必要としません。 – Pixelf

0

関数には独自の変数スコープがあります。これは多くの言語で当てはまります。つまり、関数の実行が終了すると、変数は存在しなくなります(Pythonのガーベジコレクションはそれらをクリーンアップします)。

これを行う古い学校(一般的には、面倒ではありませんが、必ずしも正式ではありません)は、グローバル変数を使用することです。これらは、通常はソースの冒頭にある関数の範囲外で宣言した変数で、プログラムのさまざまな関数やクラス全体で使用できます。

パフォーマンスの問題からローカルスコープの変数と混同するまでグローバル変数を使用しない理由はありますが、情報を保存してプログラム全体にアクセスするためのすばやく簡単な方法です。

グローバルを使用するには、あなたはこのように、その変数を使用している関数内で宣言する必要があります:

MyGlobal="This is a global variable" 
def MyFunction(): 
    global MyGlobal 
    MyGlobal += " and my function has changed it" 
if __name__=="__main__": 
    print MyGlobal 
    MyFunction() 
    print MyGlobal 

これを言って、関数にしてから情報を渡すための通常の方法を使用することです引数と戻り値 - これはより良い設計方法論であり、通常教えられているものです。これは、コードの変更よりも設計方法です。グローバル変数を絶​​対最小値に抑えてプログラムを書いてください。

上記の例を取るために、これは次のように我々のコードを変更します:これははるかに柔軟であること

def MyFunction(MyArg): 
    MyArg+=" and my function has given me a new version of it" 
    return MyArg 
if __name__=="__main__": 
    MyVariable="This is a variable" 
    print MyVariable 
    MyVariable = MyFunction(MyVariable) 
    print MyVariable 

注 - 私は上記の持っているように私は、MYVARIABLEの値を変更するには、それを使用することができます同じ関数を使って新しい値を別の変数に返すこともでき、元のままにしておくことができます。

私はこれが役立つことを願っています、もし私が少し冗長であれば申し訳ありません。

関連する問題