2015-05-01 132 views
-1

我想調用我在另一個函數中的函數內創建的列表,但是我得到列表未定義的錯誤。當我試圖做Menu = The_menu()時,它在函數內運行while循環。當我打電話Restaurant_str從函數內返回一個列表

from collections import namedtuple 
Restaurant = namedtuple('Restaurant', 'name cuisine phone menu') 
Dish = namedtuple("Dish", "name price calories") 


# Constructor: r1 = Restaurant('Taillevent', 'French', '01-11-22-33-44', 'Escargots', 23.50) 

def Restaurant_str(self: Restaurant) -> str: 
    return (
     "Name:  " + self.name + "\n" + 
     "Cuisine: " + self.cuisine + "\n" + 
     "Phone: " + self.phone + "\n" + 
     "Menu: " + Dishlist_display(Menu)) 

def Restaurant_get_info() -> Restaurant: 
    """ Prompt user for fields of Restaurant; create and return. 
    """ 
    return Restaurant(
     input("Please enter the restaurant's name: "), 
     input("Please enter the kind of food served: "), 
     input("Please enter the phone number: "), The_menu()) 

def The_menu(): 
    """ Creates the menu that replaces dish in Restaurant. 
    """ 
    Menu = [] 
    while True: 
     x = input("Please enter the name of a Dish: ") 
     if x == "No more": 
      break 
     y = float(input("Please enter the price of that Dish: ")) 
     z = float(input("Please enter the calorie count of that Dish: ")) 
     Menu.append(Dish(x, y, z)) 
    return Menu 


def Dish_str(x:Dish) -> str: 
    """ Takes a Dish and returns a string with name, price, and calories listed. 
    """ 
    return (x.name + " ($" + str(x.price) + "): " + str(x.calories) + " cal") 

def Dishlist_display(x:list) -> str: 
    """Takes a list of Dishes and returns one large string consisting of the  string representation of each dish followed by a newline character 
    """ 
    dish = "" 
    for i in x: 
     dish = dish + Dish_str(i) + ("\n") 
    return dish 

這裏的回溯出現該問題,

Traceback (most recent call last): 
    File "C:\Users\jayjay\Documents\ICS 31\Lab 5\restaurantsd.py", line 193,  in <module> 
restaurants() 
    File "C:\Users\jayjay\Documents\ICS 31\Lab 5\restaurantsd.py", line 15, in restaurants 
    our_rests = handle_commands(our_rests) 
    File "C:\Users\jayjay\Documents\ICS 31\Lab 5\restaurantsd.py", line 43, in handle_commands 
    print(Collection_str(C)) 
    File "C:\Users\jayjay\Documents\ICS 31\Lab 5\restaurantsd.py", line 128, in Collection_str 
    s = s + Restaurant_str(r) 
    File "C:\Users\jayjay\Documents\ICS 31\Lab 5\restaurantsd.py", line 76, in Restaurant_str 
    "Menu: " + Dishlist_display(Menu)) 
NameError: name 'Menu' is not defined 
+1

你應該標記的編程語言,你想引用變量()。這看起來像Python嗎? – user1032613

+0

哦好吧謝謝@ user1032613 – jjm318

+1

你能複製並粘貼實際的回溯? – logic

回答

3

的問題是,你從裏面Restaurant_str調用Dishlist_display(Menu),但你沒有一個變量名爲Menu功能。

事實上,你碰巧在另一個函數中有一個名稱的局部變量對你沒有任何好處。一旦其他函數返回,該變量就消失了。

你需要做的是從The_menu返回值並將其從一個函數傳遞到下一個函數,或者將其存儲在需要它的每個人都可以找到的地方。

如果你以正常的OO風格寫這個,那麼正確的做法是將它存儲在Restaurant實例的一個屬性中,這些屬性都是這些函數的成員。如果你真的想用(非OCa)ML fake-OO風格笨拙地移植到Python,就好像你正在做的那樣,正確的做法是將它隱藏在代表對象的閉包中的非局部變量中。如果你想要一個純粹的功能風格,那麼正確的做法是將它從功能傳遞到功能。但是您必須執行某件事才能將The_menu返回的值轉換爲Restaurant_str可以訪問的值。

+0

好吧謝謝,我看到我做得更復雜了,它應該是是。我會像你說的那樣去做 – jjm318

+0

讓我在這裏重申一點 - 如果它是面向對象的,這會更好。 – Joel

+0

@Joel:其實,我懷疑你可以設計這個應用程序沒有面向對象。更重要的是,如果你用OO術語來設計它,那麼你應該使用Python的面向對象特性,而不是試圖從頭開始。一旦你開始拋出方法以外的自我聲明,現在是時候備份並詢問爲什麼它們不是方法。 – abarnert

0

看起來你想引用一個未知變量「Menu」而不是調用構建菜單的函數。

"Menu: " + Dishlist_display(Menu)) 

應該讀

"Menu: " + Dishlist_display(The_menu())) 

或者,您可以創建使用菜單= The_menu

+0

好的謝謝,我會那麼做 – jjm318