Defult (startup) Variables?

Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Aug 2007
Posts: 59
Reputation: Seagull One is an unknown quantity at this point 
Solved Threads: 0
Seagull One Seagull One is offline Offline
Junior Poster in Training

Defult (startup) Variables?

 
0
  #1
Jul 23rd, 2008
Hello again, everyone.

My program for my robot is progressing. Thanks to your help its coming along great. I've also found that I'm more and more able to solve my own problems with programming now.

One thing that I can't seem to solve:

I'm working on a part of my robot's human-socialization program in which she keeps track of the topic, and then adds her own input. For example, if I talk to my robot about Disneyland, she'll keep track of different aspects of the theme park. I do this with a variable, say for each park, and if one of the varibles is greater than 3, she'll say something like, "You seem to like Main Street a great deal." That part works more or less fine.

But when I first start up the program and mention a ride at Disneyland, such as "Rail Road" I get this exception:

<type'exceptions.AttributeError'>:'MyApp' object has no attribute 'topic' at line 1.

This is the code associated with the recognized speech "Rail Road":

  1. if self.topic == "disneyland":
  2. speaker.Speak(random.choice(DisneyRideTrain))
  3. else:
  4. speaker.Speak('Cool. What rail road did you ride')

I know what the exception means: it means that unless I say change the topic to "disneyland" this snippet won't work because the variable "self.topic" doesn't have anything associated with it. So what I need is a default, or startup, value for "self.topic."

As simple as it sounds, I don't know how that would work. I've tried adding code like:

  1. self.topic = "nothing"
  2. print self.topic

under the class "MySpeechParser," which holds "self.topic"

But when I run the program, nothing seems to change. Even if I write the same code outside the MySpechParser class.

Anyone have any ideas? Thanks!
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 311
Reputation: BearofNH is on a distinguished road 
Solved Threads: 40
BearofNH's Avatar
BearofNH BearofNH is offline Offline
Posting Whiz

Re: Defult (startup) Variables?

 
0
  #2
Jul 23rd, 2008
The normal way to handle this is in the init function inside class MyApp, you would write self.topic=None or something similar.
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 59
Reputation: Seagull One is an unknown quantity at this point 
Solved Threads: 0
Seagull One Seagull One is offline Offline
Junior Poster in Training

Re: Defult (startup) Variables?

 
0
  #3
Jul 23rd, 2008
Ugh (head bonk) of course! That's why is says "MyApp" in there.

Okay, I added it under MyApp. It took a little while to debug a few things along the way, but it works great now!

Thanks!
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Other Threads in the Python Forum


Views: 401 | Replies: 2
Thread Tools Search this Thread



Tag cloud for Python
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC