2017-03-29 347 views
0

我會開始說我查找了這個問題的答案,不幸的是,我只是無法理解他們,或者他們似乎沒有爲我工作。這當然歸結於我而不是回答問題的人,所以我提前道歉。從一個函數調用變量到另一個函數(Python)

所以幾乎我試圖調用由用戶指定從一個功能到另一個變量,我舉一個例子:

def function_one(): 
    a = input("What is your name?") 
    return a 
def function_two(): 
    print("Nice to meet you, "a) 

function_one() 
function_two() 

這當然不工作,我我確信這是我自己的愚蠢,我不知道爲什麼起初,因爲我看到其他人說簡單地返回變量,我做了!

我也嘗試從另一個函數調用的變量,例如:

def function_two() 
    a = function_one() 

但我意識到這是很愚蠢的,因爲我只是在分配該功能作爲,所以它不是去上班。

我很感激一些見解,我知道這些並不是你所期待的那種問題,但是......我是無能的。

+2

_「但我意識到[...],所以它不會起作用。」但是你真的運行它,看它是否工作?如果沒有,我建議現在嘗試;-) – Kevin

+0

你的第二個打印語句有問題。由於您沒有識別出您遇到的錯誤,因此很難判斷這是抄錄錯誤還是實際問題的一部分。 – glibdud

+0

第二個函數(第二個函數內部的調用函數)將起作用,並不是那麼糟糕。爲什麼第一個不工作:它返回輸入,但你不保存該輸入,你必須將它分配給一些變量:'a = function_one()'。對於第二個函數,您需要將存儲在a中的值傳遞給函數,或者使用'global a'來使用全局變量。要傳遞該函數:'def function_two(a)'將意味着它將變量'a'作爲參數,並將其稱爲'function_two(a)'將給出結果。你也可以做一些像'function_two(function_one())' –

回答

2

我想你想要做的是採取用戶的輸入,將其存儲在一個變量,然後使用該變量問候用戶:

def ask_for_users_name(): 
    name = input("What is your name?") 
    return name 
def greet_user(name): 
    print("Nice to meet you, " + name) 

users_name = ask_for_users_name() 
greet_user(users_name) 

需要注意的一個重要的事情是,我不得不級聯名稱字符串「很高興見到你」,使用+運算符。

編輯:

要回答在評論這個問題,你可以做這樣的事情在這種情況下:

def ask_for_user_info(): 
    name = input("What is your name?") 
    age = input("What is your age?") 
    return name, age 

user_name, user_age = ask_for_user_info() 

最佳做法是使函數只做一兩件事,對於很多原因之一是,該功能的名稱通常取代了內聯註釋任何需要:

def ask_for_user_name(): 
    name = input("What is your name?") 
    return name 
def ask_for_user_age(): 
    age = input("What is your age?") 
    return age 

在ask_for_user_info()方法的情況下,不立即CL耳朵從名字上究竟做了什麼。

編輯2:

然後,您可以使用這兩個功能,這樣,以任何順序:

age = ask_for_user_age() 
name = ask_for_user_name() 

name = ask_for_user_name() 

age = ask_for_user_name() 
+0

啊,這很有道理。但是如果函數'ask_for_users_name'裏面還有其他事情發生了(我知道這可能是壞習慣,但是理論上),如果它在第一個函數中詢問了你的年齡等,當分配變量時不會重複users_name到那個函數? –

+0

嘿好的問題,我會編輯原始帖子來回答你的問題。我真的不能在代碼中張貼好代碼 – Outis

+0

對不起,對此我感激不盡,但我還有一個問題。我知道你想打電話給他們,但是如果你有姓名和年齡,但你只想打電話給年齡,而不是姓名?那麼它將如何工作?因爲我在這裏看到的我不能稱呼年齡而不叫名字,是這樣嗎? –

0

你做有它。這應該工作。

def function_two(): 
    a = function_one() 
    print('hi {}'.format(a)) 

然後

>>>function_two() 
0

另一種方法是使用:

def ask_for_users_name(): 
     name = input("What is your name?") 
     greet_user(name) 

def greet_user(user_name): 
    print("Nice to meet you,"+user_name) 

ask_for_users_name() 

您只需撥打ask_for_users_name()結尾。

編輯:

greet_user_()是一個無效的功能,這意味着它不返回任何東西。在這種情況下,它所做的只是接收傳遞給它的輸入並將其打印出來。如果你想執行其他操作,你可以傳遞其他參數,或保持它的方式。

版本1:

def ask_for_users_name(): 
    name = input("What is your name?") 
    age = int(input("What is your age? ")) 
    print("Your age is ", age) 
    greet_user(name) 

def greet_user(user_name): 
    print("Nice to meet you,"+user_name) 

ask_for_users_name() 

在第1版,我們還在利用greet_user()只打印一兩件事,在ask_for_users_name執行另一個打印操作()。

版本2:

def ask_for_users_name(): 
    name = input("What is your name?") 
    age = int(input("What is your age? ")) 
    greet_user(name, age) 

def greet_user(user_name, user_age): 
    print("Nice to meet you,"+user_name) 
    print("Your age is", user_age) 

ask_for_users_name() 

在2版本中,我們通過年齡和名字greet_user(),這反過來又打印出傳遞的變量。我希望這有幫助。

+0

謝謝你的回答!然而,如果ask_for_users_name()函數也包含其他部分,說它是一個函數,它也要求你的年齡(我知道這是不好的做法),我想從函數中得到的唯一東西就是名字,那麼呢? –

+0

請在原始回覆中查看我的編輯。 – Ajax1234

相關問題