2017-08-02 17 views
-6

私は相対的な初心者であり、機能をより良く理解するのを助けようとしています。私は、単純なプログラムを書く自分のために関数内から2つの関数を呼び出す

を運動を作成しました:

  1. (各Q応答がユニークな3のいずれかによって決定されたものを、ユーザの入力に応じて、各Qに3簡単なQsの
  2. 応答し確認して下さい機能)
  3. は私がメインの「コントローラ」としての機能を使用していますエンド

で簡単な要約文にそれらの答えを入れて - (>質問)

私は)に電話して他の3つの機能を呼び出したいと思っています。

:しかし、私はどのようにわからない

(私は様々な機能で引数を入れて試してみました - - - しかし、完全に行き詰まっている(下記を参照してください)エラーを返す)私のコード私は私の関数は引数を必要とする感じを持っています
def naming(): # function to respond to name as per user input 
    if in_name == 'David': 
     print 'That\'s a cool name!!' 
    elif in_name == 'Jane': 
     print 'That\'s a great name!!' 
    else: 
     print 'That\'s an OK name!!!' 

def age(): # function to respond to age as per user input 
    if in_age > 60: 
     print 'That\'s old!!' 
    elif in_age < 15: 
     print 'That\'s young!!' 
    else: 
     print 'That\'s neither young nor old!!' 

def loc(): # function to respond to location as per user input 
    if in_loc == 'London': 
     print 'London is a big city!!' 
    elif in_loc == 'Manchester': 
     print 'Manchester is a wonderful place!!' 
    else: 
     print 'That sounds OK!!' 


def questions(): #function to own the whole process (name + age + loc) 
    in_name = raw_input('What is your name? -->') 
    naming() 
    in_age = input('How old are you? -->') 
    age() 
    in_loc = raw_input('Where do you live? -->') 
    loc() 
    print 'Your name is',in_name,', you are' ,in_age , 'years old and you live in' , in_loc,'.' 

questions() 

私はメイン質問()関数内で推測している - 私は命名/年齢/ LOC機能

内の命令または引数のいくつかのフォームを提供する必要があり、ここでいくつかの助けを本当に感謝だろう!はい - ここには他にも同様のスレッドがいくつかありますが、私はそれらを読んでいて、私には意味がありません。 理想的には、良いサマリア人が正しく動作するようにコードを編集するのに3〜4分を費やすことができるなら、私にとって最も有益なことです。

ありがとうございます!

PS - ここで私は機能questions()error

答えて

0

関数内で定義された変数は、その関数にとってローカルです。関数が定義されている時点から関数の終わりまでアクセス可能で、関数が実行されている間は存在します。

  1. あなたはそれを引数として渡す必要はありませんので、それ以外の場合は、あなたが、グローバルとして変数を定義することができ、他の機能

    def naming(in_name): # function to respond to name as per user input 
        if in_name == 'David': 
         print 'That\'s a cool name!!' 
        elif in_name == 'Jane': 
         print 'That\'s a great name!!' 
        else: 
         print 'That\'s an OK name!!!' 
    
    def age(in_age): # function to respond to age as per user input 
        if in_age > 60: 
         print 'That\'s old!!' 
        elif in_age < 15: 
         print 'That\'s young!!' 
        else: 
         print 'That\'s neither young nor old!!' 
    
    def loc(in_loc): # function to respond to location as per user input 
        if in_loc == 'London': 
         print 'London is a big city!!' 
        elif in_loc == 'Manchester': 
         print 'Manchester is a wonderful place!!' 
        else: 
         print 'That sounds OK!!' 
    
    
    def questions(): #function to own the whole process (name + age + loc) 
        in_name = raw_input('What is your name? -->') 
        naming(in_name) 
        in_age = input('How old are you? -->') 
        age(in_age) 
        in_loc = raw_input('Where do you live? -->') 
        loc(in_loc) 
        print 'Your name is',in_name,', you are' ,in_age , 'years old and you live in' , in_loc,'.' 
    
    questions() 
    
  2. に引数として値を渡すことができます。(このメソッドは推奨されません)

    def naming(): # function to respond to name as per user input 
        if in_name == 'David': 
         print 'That\'s a cool name!!' 
        elif in_name == 'Jane': 
         print 'That\'s a great name!!' 
        else: 
         print 'That\'s an OK name!!!' 
    
    def age(): # function to respond to age as per user input 
        if in_age > 60: 
         print 'That\'s old!!' 
        elif in_age < 15: 
         print 'That\'s young!!' 
        else: 
         print 'That\'s neither young nor old!!' 
    
    def loc(): # function to respond to location as per user input 
        if in_loc == 'London': 
         print 'London is a big city!!' 
        elif in_loc == 'Manchester': 
         print 'Manchester is a wonderful place!!' 
        else: 
         print 'That sounds OK!!' 
    
    
    def questions(): #function to own the whole process (name + age + loc) 
        global in_name, in_age, in_loc 
        in_name = raw_input('What is your name? -->') 
        naming() 
        in_age = input('How old are you? -->') 
        age() 
        in_loc = raw_input('Where do you live? -->') 
        loc() 
        print 'Your name is',in_name,', you are' ,in_age , 'years old and you live in' , in_loc,'.' 
    
    questions() 
    
+0

華麗!これに感謝します。本当に有益な答え - 私はほとんどそこにいました - しかし、それ以上はできませんでした。もう一度ありがとう – JasonC

0

変数は、他の機能には知られていない(またはその範囲がquestions()機能に制限されている)あなたが取得している理由、それはだそうだエラーのスクリーンショットですエラー、定義されていません

def naming(in_name): # function to respond to name as per user input 
    if in_name == 'David': 
     print 'That\'s a cool name!!' 
    elif in_name == 'Jane': 
     print 'That\'s a great name!!' 
    else: 
     print 'That\'s an OK name!!!' 

def age(in_age): # function to respond to age as per user input 
    if in_age > 60: 
     print 'That\'s old!!' 
    elif in_age < 15: 
     print 'That\'s young!!' 
    else: 
     print 'That\'s neither young nor old!!' 

def loc(in_loc): # function to respond to location as per user input 
    if in_loc == 'London': 
     print 'London is a big city!!' 
    elif in_loc == 'Manchester': 
     print 'Manchester is a wonderful place!!' 
    else: 
     print 'That sounds OK!!' 


def questions(): #function to own the whole process (name + age + loc) 
    in_name = raw_input('What is your name? -->') 
    naming(in_name) 
    in_age = input('How old are you? -->') 
    age(in_age) 
    in_loc = raw_input('Where do you live? -->') 
    loc(in_loc) 
    print 'Your name is',in_name,', you are' ,in_age , 'years old and you live in' , in_loc,'.' 

questions() 
+0

このための多くのおかげで - それはすべて銭を作ります今私にSE。非常に役に立ちました – JasonC

0

より良い方法は、それらを関数の引数として渡すことです。

def naming(in_name): # function to respond to name as per user input 
    if in_name == 'David': 
     print 'That\'s a cool name!!' 
    elif in_name == 'Jane': 
     print 'That\'s a great name!!' 
    else: 
     print 'That\'s an OK name!!!' 

def questions(): #function to own the whole process (name + age + loc) 
    in_name = raw_input('What is your name? -->') 
    naming(in_name) 

再び変数in_age、それはraw_inputstring形式にデフォルトであるようintに変換する必要があります。

in_age = input('How old are you? -->') 
age(int(in_age)) 
+0

グローバル定義に関するアドバイスが悪いので、私はダウンしました。これは 'NameError'を解決しますが、実際に問題を解決するわけではありません(変数を関数に渡す)。一般的にこのようなグローバルスコープを使用するのは避けてください(変数が設定定数でない場合)。 –

+0

@Robin。はい、それは本当だ。私は前にコードをテストしていません。その部分を削除しました。 – SunilT

関連する問題