2012-11-11 72 views
1

我對編程非常陌生。我一直試圖通過一本名爲「絕對初學者的Python編程」的書來學習Python。我正在上課。我從書中的一個練習中複製了一些代碼,並得到了一個Traceback(最近一次調用最後):和NameError消息。以下是我與代碼相關的錯誤消息。請幫忙。謝謝!Python跟蹤和NameError

Traceback (most recent call last): 
File "C:\Python27\Ch8 Critter Caretaker Prg.py", line 5, in <module> 
    class Critter(object): 
File "C:\Python27\Ch8 Critter Caretaker Prg.py", line 25, in Critter 
    mood = property(__get_mood) 
NameError: name '_Critter__get_mood' is not defined 
# Critter Caretaker 
# A virtual pet to care for 

class Critter(object): 
"""A virtual pet""" 
    def __init__(self, name, hunger = 0, boredom = 0): 
     self.name = name 
     self.hunger = hunger 
     self.boredom = boredom 
    def __pass_time(self): 
     self.hunger += 1 
     self.bordedom += 1 
    def __get_mode(self): 
     unhappiness = self.hunger + self.boredom 
     if unhappiness < 5: 
      mood = "happy" 
     elif 5 <= unhappiness <= 10: 
      mood = "okay" 
     elif 11 <= unhappiness <= 15: 
      mood = "frustrated" 
     else: 
      mood = "mad" 

    mood = property(__get_mood) 

    def talk(self): 
     print "I'm", self.name, "and I feel", self.mood, "now.\n" 
     self.__pass_time() 

    def eat(self, food = 4): 
     print "Brrupp. Thank you." 
     self.hunger -= food 
     if self.hunger < 0: 
      self.hunger = 0 
     self.__pass_time() 

    def play(self, fun = 4): 
     print "Wheee!" 
     self.boredom -= fun 
     if self.boredom < 0: 
      self.boredom = 0 
     self.__pass_time() 

def main(): 
    crit_name = raw_input("What do you want to name your critter?: ") 
    crit = Critter(crit_name) 

    choice = None 
    while choice != "0": 
     print \ 
     """ 
     Critter Caretaker 

     0 - Quit 
     1 - Listen to your critter 
     2 - Feed your critter 
     3 - Play with your critter 
     """ 

     choice = raw_input("Choice: ") 
     print 

     # exit 
     if choice == "0": 
      print "Good-bye." 

     # listen to your critter 
     elif choice == "1": 
      crit.talk() 

     # feed your critter 
     elif choice == "2": 
      crit.eat() 

     # play with your critter 
     elif choice == "3": 
      crit.play() 

     # some unknown choie 
     else: 
      print "\nSorry, but", choice, "isn't a vaild choice." 

main() 
("\n\nPress the enter key to exit.") 
+1

如果這本書推薦使用領先的雙下劃線,那麼小心處理它。有沒有解釋爲什麼代碼不只使用一個下劃線? – Matthias

回答

0

這是一個簡單的拼寫錯誤。您可以定義

def __get_mode(self): 
#   ^^^ 

但進入

_Critter__get_mood 
#    ^^^ 
2

前面已經提到的,你modemood之間做了一個錯字。但是,稍後您會遇到更多問題 - 您的__get_mood函數從來沒有真正獲得心情,因爲它永遠不會返回。此外,您還可以使用property作爲裝飾:

@property 
def mood(self): 
    unhappiness = self.hunger + self.boredom 
    if unhappiness < 5: 
     return "happy" 
    elif 5 <= unhappiness <= 10: 
     return "okay" 
    elif 11 <= unhappiness <= 15: 
     return "frustrated" 
    else: 
     return "mad" 
1

看起來你打算定義__get_mode(self)__get_mood(self)代替。

提供有關一些澄清NameError你得到時,解釋說,_Critter__get_mood is not defined而不是__get_mood is not defined因爲name mangling,這是一些Python花式來指示類private變量或方法。