2017-03-07 201 views
2

所以我前幾天做了一項學費課程作業。這是我的程序。我希望這是自我解釋。 (這是Python的,順便說一句。)Python:「名稱未定義」錯誤

def print_intro(): 
print ("Welcome to the tuition program. This program will calculate your tuition cost.") 

def get_id_num(): 
    grad_status = input("Is the student an undergrad or grad (U/G)? ") 
    id_num = int(input("Enter the student's ID Number: ")) 
    while (grad_status == "U") and (id_num < 0): 
     print("Sorry, that is invalid. Please try again.") 
     id_num = int(input("Enter the student's ID Number: ")) 
    while (grad_status == "G") and (id_num >= 0): 
     print("Sorry, that is invalid. Please try again.") 
     id_num = int(input("Enter the student's ID Number: ")) 

def get_info(): 
    get_id_num() 
    age = int(input("Enter the student's age (numbers only): ")) 
    residency = input("Is the student in-state or out-of-state (I/O)? ") 
    num_credits = int(input("How many hours of credit will the student take? ")) 
    correct = input("Is all the above information correct (Y/N)? ") 
    while correct == "N": 
     print("Please enter the information correctly!") 
     get_info() 
    else: 
     get_base_tuition() 

def get_base_tuition(): 
    if (grad_status == "U") and (num_credits <= 6) and (residency == "I"): 
     initial_tuition = 2000 
    if (grad_status == "U") and (7 <= num_credits <= 11) and (residency == "I"): 
     initial_tuition = 3000 
    if (grad_status == "U") and (num_credits >= 12) and (residency == "I"): 
     initial_tuition = 3800 
    if (grad_status == "U") and (num_credits <= 6) and (residency == "O"): 
     initial_tuition = 2000 
    if (grad_status == "U") and (7 <= num_credits <= 11) and (residency == "O"): 
     initial_tuition = 3000 
    if (grad_status == "U") and (num_credits >= 12) and (residency == "O"): 
     initial_tuition = 9000 
    if (grad_status == "G") and (num_credits <= 6) and (residency == "I"): 
     initial_tuition = 2000 
    if (grad_status == "G") and (7 <= num_credits <= 11) and (residency == "I"): 
     initial_tuition = 2500 
    if (grad_status == "G") and (num_credits >= 12) and (residency == "I"): 
     initial_tuition = 4400 
    if (grad_status == "G") and (num_credits <= 6) and (residency == "O"): 
     initial_tuition = 2000 
    if (grad_status == "G") and (7 <= num_credits <= 11) and (residency == "O"): 
     initial_tuition = 3700 
    if (grad_status == "G") and (num_credits >= 12) and (residency == "O"): 
     initial_tuition = 4400 
    print(initial_tuition) 

於是我運行此之後,中的get_info()函數運行正常,直到它進入get_base_tuition()函數。我得到這個錯誤:

line 28, in get_base_tuition 
    if (grad_status == "U") and (num_credits <= 6) and (residency == "I"): 
NameError: name 'grad_status' is not defined 

我應該在我的函數中添加參數嗎?我被卡住了,真的不知道該怎麼辦。我知道,如果我在get_base_tuition()中包含變量「grad_status」,「num_credits」和「residency」,或者在運行get_base_tuition()之前在Python Shell中定義變量,那麼它當然會起作用。但是,如果我像現在這樣把它放在另一個get_base_tuition()中,它將不起作用。有沒有辦法讓get_base_tuition()函數運行,而不必將變量放在get_base_tuition()中?因爲我已經將這些變量放在函數get_id_num()和get_info()中。 幫助表示讚賞,謝謝。

在你想了解的基礎學費是應該的計算方式的情況下,我是連接分配這裏: Assignment Page 2

回答

3

你需要閱讀和理解的範圍在Python的概念。這裏有一個很好的答案,用於獲得basics on scope

特別是在您的代碼中,grad_status變量是在get_id_num函數中定義的。一旦程序退出該函數,該範圍中定義的所有變量將被刪除,並且不會存在於其他函數的範圍內。如果您有多個函數需要的變量,則需要在全局範圍中定義該變量,或將其傳遞到需要它作爲參數的每個函數中。

+0

只是想澄清,當我說的變量是「刪除」,這並不一定意味着他們會立即處理。這是一個重要的區別,對於需要明確關閉的資源(如數據庫連接)尤其如此。 – gph

+0

真的有幫助!我在全球範圍內定義了我的變量,並且一切正常!真的很感謝幫助! –