2017-08-02 53 views
-6

我是一個相對的新手,並且試圖幫助我更好地理解函數。我已經創建了一個鍛鍊自己從一個函數內調用2個函數

寫一個簡單的程序:

  1. 向3點簡單的Qs的這取決於用戶輸入
  2. 應對每個Q(每個Q響應由3個獨特的一個確定功能)
  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 - 這裏是我得到 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()是不知道的其他功能(或者其範圍僅限於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

非常感謝這一點 - 這一切使得仙現在就對我說。非常有幫助 – 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,它需要轉換爲intraw_input默認處於string格式。

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

我因爲關於全局定義的建議是不好的imo而失敗了。它解決了'NameError',但並沒有真正解決問題(將一個變量作爲一個函數傳遞給它),並且通常應該避免使用這樣的全局範圍(如果變量不是某個配置常量)。 –

+0

@Robin。對,是真的。我以前沒有測試過代碼。刪除了該部分。 – SunilT

相關問題