2016-04-08 7 views
1

その関数外の関数で定義された変数の値を取得する方法が必要です。それは必要ではありませんが、私のコードを非常に簡単に従わせるでしょう。まだこれに対する確かな答えは見つかりませんでした。Python:関数内で定義された変数の値の取得

def pizzais(): 
    pizza = "yummy" 

pizzais() 
print(pizza) 

これは、ピザが定義されていないというエラーを返します。これを回避するためのハックはありますか?

ここで私の状況をよく理解することが私のコードです。私もそれを適用しています。

def questions(): 
    user = input("What is your username?") #username 
    race = input("What is your race? (orc, human, elf, goblin)") #race 

#won't move on if the player fills an answer that is not an option 
    if race == "orc" or race == "human" or race == "elf" or race == "goblin": 
     pClass = input("What is your class? (archer, warrior, rogue or mage)") 


    else: 
     while race != "orc" and race != "human" and race != "goblin" and race != "elf": 
      race = input("What is your race? (orc, human, elf, goblin)") 
      if race == "orc" or race == "human" or race == "elf" or race == "goblin": 
       pClass = input("What is your class? (archer, warrior, rogue or mage)") 

#won't move on if the player fills an answer that is not an option    
    if pClass == "archer" or pClass == "warrior" or pClass == "rogue" or pClass == "mage": 
      correct = input("So you are " + user + ", the " + race + " " + pClass + "? (yes or no)") 

    else: 
     while pClass != "archer" and pClass != "warrior" and pClass != "rogue" and pClass != "mage": 
      pClass = input("What is your class? (archer, warrior, rogue or mage)") 
      if pClass == "archer" or pClass == "warrior" or pClass == "rogue" or pClass == "mage": 
       correct = input("So you are " + user + ", the " + race + " " + pClass + "? (yes or no)") 

    def correct_def(): 
     correct = input("So you are " + user + ", the " + race + " " + pClass + "? (yes or no)") 
     if correct == "yes": #if player likes their choices the game will begin 
      print("Enjoy the game " + user + "!") 

     elif correct == "no": #if player doesn't like their choices all questions are asked again 
      reAsk = input("What would you like to change?(username, race, class or all)") 

     else: 
      while correct != "yes" and correct != "no": 
       correct = input("So you are " + user + ", the " + race + " " + pClass + "? (yes or no)") 
      if correct == "yes": 
       print("Enjoy the game " + user + "!") 

      elif correct == "no": 
       questions() 

    if correct == "yes": #if player likes their choices the game will begin 
     print("Enjoy the game " + user + "!") 

    elif correct == "no": #if player doesn't like their choices all questions are asked again 
     reAsk = input("What would you like to change?(username, race, class or all)") 

     if reAsk == "username": 
      user = input("What is your username?") 
      correct_def() 

     elif reAsk == "race": 
      race = input("What is your race? (orc, human, elf, goblin)") 
      while race != "orc" and race != "human" and race != "goblin" and race != "elf": 
       race = input("What is your race? (orc, human, elf, goblin)") 
      correct_def() 

     elif reAsk == "class": 
      pClass = input("What is your class? (archer, warrior, rogue or mage)") 
      while pClass != "archer" and pClass != "warrior" and pClass != "rogue" and pClass != "mage": 
       pClass = input("What is your class? (archer, warrior, rogue or mage)") 
      correct_def() 

     elif reAsk == "all": 
      questions() 

     else: 
      while reAsk != "username" and reAsk != "race" and reAsk != "class" and reAsk != "all": 
       reAsk = input("What would you like to change?(username, race, class or all)") 
       if reAsk == "username": 
        user = input("What is your username?") 
        print("Enjoy the game " + user + "!") 

       elif reAsk == "race": 
        race = input("What is your race? (orc, human, elf, goblin)") 
        while race != "orc" and race != "human" and race != "goblin" and race != "elf": 
         race = input("What is your race? (orc, human, elf, goblin)") 
        print("Enjoy the game " + user + "!") 

       elif reAsk == "class": 
        pClass = input("What is your class? (archer, warrior, rogue or mage)") 
        while pClass != "archer" and pClass != "warrior" and pClass != "rogue" and pClass != "mage": 
         pClass = input("What is your class? (archer, warrior, rogue or mage)") 
        print("Enjoy the game " + user + "!") 

       elif reAsk == "all": 
        questions() 

#won't move on if the player fills an answer that is not an option 
    else: 
     while correct != "yes" and correct != "no": 
      correct = input("So you are " + user + ", the " + race + " " + pClass + "? (yes or no)") 
     if correct == "yes": 
      print("Enjoy the game " + user + "!") 

     elif correct == "no": 
      reAsk = input("What would you like to change?(username, race, class or all)") 

     if reAsk == "username": 
      user = input("What is your username?") 
      correct_def() 

     elif reAsk == "race": 
      race = input("What is your race? (orc, human, elf, goblin)") 
      while race != "orc" and race != "human" and race != "goblin" and race != "elf": 
       race = input("What is your race? (orc, human, elf, goblin)") 
      correct_def() 

     elif reAsk == "class": 
      pClass = input("What is your class? (archer, warrior, rogue or mage)") 
      while pClass != "archer" and pClass != "warrior" and pClass != "rogue" and pClass != "mage": 
       pClass = input("What is your class? (archer, warrior, rogue or mage)") 
      correct_def() 

     elif reAsk == "all": 
      questions() 

     else: 
      while reAsk != "username" and reAsk != "race" and reAsk != "class" and reAsk != "all": 
       reAsk = input("What would you like to change?(username, race, class or all)") 
       if reAsk == "username": 
        user = input("What is your username?") 
        print("Enjoy the game " + user + "!") 

       elif reAsk == "race": 
        race = input("What is your race? (orc, human, elf, goblin)") 
        while race != "orc" and race != "human" and race != "goblin" and race != "elf": 
         race = input("What is your race? (orc, human, elf, goblin)") 
        print("Enjoy the game " + user + "!") 

       elif reAsk == "class": 
        pClass = input("What is your class? (archer, warrior, rogue or mage)") 
        while pClass != "archer" and pClass != "warrior" and pClass != "rogue" and pClass != "mage": 
         pClass = input("What is your class? (archer, warrior, rogue or mage)") 
        print("Enjoy the game " + user + "!") 

       elif reAsk == "all": 
        questions() 
questions() 
+2

なぜあなたは「ハックする」でしょうか?これが 'return'の目的です。 – tzaman

+2

あなたの質問をよりよく理解するために、コードのどの部分を見ていなければなりませんか?あなたの問題はどこですか? – Keatinge

+0

基本的には、質問の外にrace、pClass、usernameを使用できるようにしたいです。 – Shniper

答えて

2

おそらくTutorial section on functionsを読んでください。これは、の機能を使用する最も基本的なことです。

あなたのコードサンプルは巨大な混乱であるが、ここであなたがやろうとしているように見えるものの簡単な例です:

:あなたの他のオプションについては

def ask_race(): 
    race = None 
    options = ['orc', 'human', 'elf', 'goblin'] 
    while race not in options: 
     race = input('What is your race? ({})'.format(','.join(options))) 
    return race 

を繰り返し、その後、あなたのような何かを行うことができます

def questions(): 
    race = ask_race() 
    character_class = ask_class() 

クリーンアップの方がはるかに多いですが、これはやや穏やかな構造から始めるべきです。

+0

ええ、私はそれが混乱していることを知っている、私は自分自身を教えて、教授から得られた非常に限られた知識と私のクラスのいずれかのためのpythonゲームを作成する必要があります。だから私の周りでは、jsをPythonに変える方法があります。申し訳ありません – Shniper

+1

'class'は有効な変数名ではないので、2番目のスニペットは無効です –

+0

Oh derp、fixed。 – tzaman

2

これを行うには2つの方法があり、1つはあまりお勧めしません。 あなたが何をしているのか分からない限り、グローバル変数を使いこなすべきではありません。あなたがしようとしていることは、100%がリターンを使うべきです。しかし包括的であるためには、となる可能性があるため、理論的に達成しようとしています。

ベストプラクティス:リターンを使用して

def pizzais(): 
    return "yummy" 

pizza = pizzais() 
print(pizza) 

悪いアイデア:使用してグローバル変数

pizza = "" 

def pizzais(): 
    global pizza 
    pizza = "yummy" 


pizzais() 

print(pizza) 

戻る複数の変数

def get_three_variables(): 
    var1 = "text1" 
    var2 = "text2" 
    var3 = "text3" 
    return (var1, var2, var3) 


response = get_three_variables() 
print(response) 
#this prints: ('text1', 'text2', 'text3') 

print(response[0]) 
#this prints text1 

print(response[1]) 
#this prints text2 
+0

私は関数内にいくつかの変数を持っていて、関数内の各変数を返すとどうすれば関数の外でそれらの変数のそれぞれの値を得ることができますか? – Shniper

+0

は複数の変数からなるタプルを返す – Keatinge

+2

技術的には、 'global'は逆です - 内部で定義された変数を使用しています。 – tzaman

0

pizzaを印刷しようとしていますが、ピザとは何ですか?あなたはあなたの関数を除いてどこにでも定義しておらず、何も返さない。 ローカルの変数ですが、関数の範囲外のピザを印刷しようとすると、というグローバル変数が検索されます。ローカル変数とグローバル変数については、questionを参照してください。機能の最後にreturn "yummy"を設定すると、pizza = pizzais()を設定してからprint(pizza)に設定することができます。関数の最後にreturnを使用しないと、デフォルトでNoneが返されます。

1
  1. 複数の値をタプルreturn x, y, zで返し、戻り値をx, y, z = func()のようにアンパックします。
  2. グローバル変数を使用できます。関数内で値を設定するときは、最初にglobal xと宣言し、次にx = somethingを宣言します。しかしグローバル変数は避けるのが最善です。
  3. あなた自身がグローバルな状態をたくさん変えていることがわかった場合は、classを使用する方が適切です。
  4. クロージャを使用して状態を維持することができます。しかし、それは異なったデザインを必要とし、あなたが高次関数に慣れているならばそれを行うべきです。クロージャは原則として軽量であり、クラスよりはるかに高速です(Beazly)。
関連する問題