2014-12-27 68 views
-4

我有一些代碼我亂搞..非常新的python和編碼一般,但是我難住這一個。蜇傷回來名稱錯誤

我定義了一個函數,並從中獲得了一些用戶輸入。當我嘗試打印輸入時,出現NameError:name'my_name'未定義。

這裏是代碼..

#New Program 
    #Written by Chris Nelson of Cnelson Creations 

    #This code obtains some basic information from the user 
    def questions(): 
    if userinput == "Y": 
     my_name = input("What is your name? ")`enter code here` 
     my_age = input("What is your age? ") 
     my_urname = input("Please pick a username.. ") 
     my_psword = input("Please pick a password.. ") 
    elif userinput == "N": 
     print ("Sorry we need the information to continue..") 
     print ("... Goodbye") 
     exit() 
    else: 
     print ("Not a valid choice.. Goodbye!") 
     exit() 
    print ("We need to obtain some information from you before going forward") 
    print ("Is this ok? Enter Y for 'yes' or N for 'No'...") 
    userinput = input("Please enter Y or N ") 
    questions() #This code runs the function listed above.. 
    print ("Great we have some information from you!") 
    print ("Lets confirm it is the correct information.. ") 
    print ('{}, {}, {}, {}, is this information correct?'.format(my_name, my_age, my_urname, my_psword)) 
+0

你有** **範圍問題。 – keyser 2014-12-27 16:22:13

+0

如果可以,你能否詳細說明一下?謝謝,克里斯。 – 2014-12-27 16:23:32

回答

2

您定義my_name一個函數,它會進入本地範圍內。看看這個問題的Short Description of the Scoping Rules?。你有多種方式來解決這個

  • my_name,並在函數中定義可見你必須定義它們的功能之外的其他變量。

  • 或添加全局聲明你的函數

    global my_name,my_age,my_urname,my_psword

  • 內部或使用著名元組封裝和序列拆封

    有一個說法在你if狀況在年底功能

    return (my_name,my_age,my_urname,my_psword) 和h在您的函數調用中,您可以使用以下功能:ave my_name,my_age,my_urname,my_psword = questions()

  • 還是在功能本身打印值,就像你有你自己提出

+1

啊,我明白了.. 或者只是在功能裏面打印顯示那個信息? – 2014-12-27 16:24:53

+0

或將它們作爲參數傳遞給函數。 – Mhmd 2014-12-27 16:26:48

+0

回報是一種更好的方式+1 – 2014-12-27 16:36:47

-3

添加全球MY_NAME,my_age,my_urname,my_psword

def questions(): 
    global my_name,my_age,my_urname,my_psword 
    if userinput == "Y": 
     my_name = input("What is your name? ")`enter code here` 
     my_age = input("What is your age? ") 
     my_urname = input("Please pick a username.. ") 
     my_psword = input("Please pick a password.. ") 

所以,你可以使用變量功能之外。

+0

總有比全球更好的方法 – 2014-12-27 16:31:17

+0

那麼我們應該警告Guido van Rossum關於從Python中刪除_global_的問題嗎?例如eval()是一個危險的函數,但是如果你知道如何使用它,那就沒有問題了。 – GLHF 2014-12-27 16:32:35

+0

但是他應該學習_global_也做什麼,並且它是一個很好的答案在我看來,正如你看到所有其他答案一樣。 – GLHF 2014-12-27 16:34:14

0

因爲您試圖在功能外打印my_name。正如你在函數內部蔑視它,所以它在本地(作用域)名稱空間中()函數中的變量具有局部作用域(意味着你不能在函數之外和其他作用域上訪問它們))!如果你想在函數外訪問它,你需要將它定義爲global。通過在你的函數的頂層添加global my_name,同樣當你想訪問另一個變量時,你需要爲這些人做些類似my_name的事情。

但作爲愛茉莉有效的方式,你可以回到你的函數裏面那些變量:

def questions(): 
    if userinput == "Y": 
     my_name = input("What is your name? ")`enter code here` 
     my_age = input("What is your age? ") 
     my_urname = input("Please pick a username.. ") 
     my_psword = input("Please pick a password.. ") 
    elif userinput == "N": 
     print ("Sorry we need the information to continue..") 
     print ("... Goodbye") 
     exit() 
    else: 
     print ("Not a valid choice.. Goodbye!") 
     exit() 
    return (my_name,my_age,my_urname,my_psword) 

,你可以做以下的分配之外的功能:

my_name, my_age, my_urname, my_psword= questions()