User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the Python section within the Software Development category of DaniWeb, a massive community of 363,829 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 4,187 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Python advertiser:
Views: 368 | Replies: 9 | Solved
Reply
Join Date: Aug 2007
Posts: 28
Reputation: Seagull One is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
Seagull One Seagull One is offline Offline
Light Poster

True and False Problem

  #1  
May 15th, 2008
This problem has been bugging me for the past few days. I've tried all sorts of different things to get it to work, studied my books, but nothing.

It goes like this:
Let's say I want my robot to recite the three laws of robotics. I can say, "Recite the three laws of robotics," and the computer will recognize my speech and recite them perfectly via TTS. Now, let's say that this time I want to ask my robot if it knows the three laws? My robot will say "Yes, I know them well. Would you like me to recite them for you?"
Right after it says this a variable in the script called LawRecite changes to True. I know it is changing to True in my script because I have this code snipet:
speaker.Speak ('Yes, I know them well. Would you like me to recite them for you?')
LawRecite = True
if LawRecite == True:
print 'Lets talk about the three laws of Robotics'
and that string in the end is coming up, so I know nothing's wrong there.

The problem is, when I say "Yes please," which has this code snipet attatched to it:

while LawRecite == True:
speaker.Speak(LOR1)
speaker.Speak(LOR2)
speaker.Speak(LOR3)
speaker.Speak(random.choice(LOR4))
else:
speaker.Speak('Yes please what?')
my robot just keeps asking, "Yes please what?" as if the value to LawRecite is still False (or something else).

Logically, if the value for LawRecite is True, it should recite strings LOR1-LOR4.
Anyone have any idea what's going on? Thanks.
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Oct 2004
Location: Mojave Desert
Posts: 2,282
Reputation: vegaseat is on a distinguished road 
Rep Power: 8
Solved Threads: 167
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
Kickbutt Moderator

Re: True and False Problem

  #2  
May 15th, 2008
Shouldn't it be ...
  1. if LawRecite == True:
  2. speaker.Speak(LOR1)
  3. speaker.Speak(LOR2)
  4. speaker.Speak(LOR3)
  5. #speaker.Speak(random.choice(LOR4))
  6. else:
  7. speaker.Speak('Yes please what?')
May 'the Google' be with you!
Reply With Quote  
Join Date: Dec 2006
Posts: 360
Reputation: woooee is on a distinguished road 
Rep Power: 2
Solved Threads: 49
woooee woooee is offline Offline
Posting Whiz

Re: True and False Problem

  #3  
May 15th, 2008
You may have more than one block of memory named "LawRecite". If you assign True in a loop or function then it is local to that loop/function and will be destroyed when you exit unless you return the value from the function. Change any variable names that are in a function and are the same as any outside the function/loop. That should clear things up. Also, "if True" and "while True" are redundant as vegaseat already pointed out. It should be
if LawRecite == True:
    print 'Lets talk about the three laws of Robotics'
    ## while LawRecite == True:   redundant
    speaker.Speak(LOR1)
    speaker.Speak(LOR2)
    speaker.Speak(LOR3)
    speaker.Speak(random.choice(LOR4))
else:
    speaker.Speak('Yes please what?')
If this doesn't help, please post more of the code in context.

Edit: This should definitely be a class. Then you can use self.LawRecite throughout and it will be the same variable. If the program is not using a class, someone here will probably help rearrange the code into the class structure.
Last edited by woooee : May 15th, 2008 at 6:10 pm.
Reply With Quote  
Join Date: Aug 2007
Posts: 28
Reputation: Seagull One is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
Seagull One Seagull One is offline Offline
Light Poster

Re: True and False Problem

  #4  
May 15th, 2008
Okay, I arranged it into a class, which I think I did right...
ThreeLaws = False

class LawRecite:
def Recite(self):
speaker.Speak(LOR1)
speaker.Speak(LOR2)
speaker.Speak(LOR3)
speaker.Speak(random.choice(LOR4))

LR = LawRecite()

Now if I ask my robot via speech recognition "Do you know the three laws of robotics?" it replies:
speaker.Speak ('Yes, I know them well. Would you like me to recite them for you?')
ThreeLaws = True

That part works fine. However, I've noticed that when I try to print if ThreeLaws is True, like so:

speaker.Speak ('Yes, I know them well. Would you like me to recite them for you?')
ThreeLaws = True
if ThreeLaws == True:
print 'Let's talk about the three laws of robotics'
else:
print 'Not right now'

I get an attribute error that says that 'NoneType' object has no attribute 'tb_lineno'.

Hmm...I think I understand what wooee meant by two blocks of memory with the same name. What I'm trying to do here is change the variable of ThreeLaws from True to False, but because I mention ThreeLaws = .... twice, that's causing it to be destroyed. Am I right? If so, what's the proper way to change a variable's value? Like a switch, I mean?
Reply With Quote  
Join Date: Dec 2006
Posts: 360
Reputation: woooee is on a distinguished road 
Rep Power: 2
Solved Threads: 49
woooee woooee is offline Offline
Posting Whiz

Re: True and False Problem

  #5  
May 16th, 2008
If it is a self.variable_name, then it is global throughout the class. I'm too tired to test this snippet so if there are other problems, post back.
class LawRecite:
   def __init__(self):
      self.ThreeLaws = False
      self.Recite()

   def Recite(self):
      ##speaker.Speak(LOR1)
      ##speaker.Speak(LOR2)
      ##speaker.Speak(LOR3)
      ##speaker.Speak(random.choice(LOR4))
      print "ThreeLaws original =", self.ThreeLaws

      self.ThreeLaws=True
      print "ThreeLaws changed =", self.ThreeLaws

      self.ThreeLaws=3
 
LR = LawRecite()
print "after changing to 3 =", LR.ThreeLaws
Last edited by woooee : May 16th, 2008 at 12:06 am.
Reply With Quote  
Join Date: Aug 2007
Posts: 28
Reputation: Seagull One is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
Seagull One Seagull One is offline Offline
Light Poster

Re: True and False Problem

  #6  
May 16th, 2008
Thanks, wooee, I added the snippet in. I've encountered a problem though:

after changing to 3 =
Traceback (most recent call last):
  File "C:\Python25\Lib\site-packages\pythonwin\pywin\framework\scriptutils.py", line 310, in RunScript
    exec codeObject in __main__.__dict__
  File "C:\Users\Loren\Desktop\My Robots\Nina Verbal Raw Input.py", line 135, in <module>
    print "after changing to 3 =",  LR.ThreeLaws
AttributeError: LawRecite instance has no attribute 'ThreeLaws'

Is the program searching for a definition for "ThreeLaws"?
Reply With Quote  
Join Date: Dec 2006
Posts: 360
Reputation: woooee is on a distinguished road 
Rep Power: 2
Solved Threads: 49
woooee woooee is offline Offline
Posting Whiz

Re: True and False Problem

  #7  
May 16th, 2008
Originally Posted by Seagull One View Post
AttributeError: LawRecite instance has no attribute 'ThreeLaws'
Is the program searching for a definition for "ThreeLaws"?
It is searching for self.ThreeLaws in the LawsRecite class. I just ran the test snippet that I posted and it works fine so check the spellings for self.ThreeLaws and LawRecite on your end. Also, you will avoid potential problems if you don't use spaces in directory or file names. "C:\Users\Loren\Desktop\My Robots\Nina Verbal Raw Input.py",
Reply With Quote  
Join Date: Aug 2007
Posts: 28
Reputation: Seagull One is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
Seagull One Seagull One is offline Offline
Light Poster

Re: True and False Problem

  #8  
May 18th, 2008
Okay, guys, its starting to work. One last thing I'm trying to fix for this issue: The moment I start my script my robot recites the three laws without me saying anything. At first the script wouldn't start up because it said (LOR1) was not defined, so I moved the class LawRecite further down the script and the then the program would run.

My robot recites the three laws on its own the first time I start the script, but after that its working like I want it to.

Is there a way the class can be adjusted so that my robot will not speak the three laws until I ask it?

By the way, I just want to say thanks to all of you on Daniweb in my quest to learn Python and programming. You guys are teaching me a lot!
Reply With Quote  
Join Date: Dec 2006
Posts: 360
Reputation: woooee is on a distinguished road 
Rep Power: 2
Solved Threads: 49
woooee woooee is offline Offline
Posting Whiz

Re: True and False Problem

  #9  
May 18th, 2008
You are probably calling self.Recite without an if statement. An example
class LawRecite:
   def __init__(self):
      self.ThreeLaws = False
      ## only if ThreeLaws is True
      if self.ThreeLaws:
         self.Recite()
Last edited by woooee : May 18th, 2008 at 6:21 pm.
Reply With Quote  
Join Date: Aug 2007
Posts: 28
Reputation: Seagull One is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
Seagull One Seagull One is offline Offline
Light Poster

Re: True and False Problem

  #10  
May 18th, 2008
Thanks guys. I added the code snippet, tinkered around a bit, and it works great!
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb Python Marketplace
Thread Tools Display Modes

Similar Threads
Other Threads in the Python Forum

All times are GMT -4. The time now is 12:11 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC