#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2004
    Posts
    5
    Rep Power
    0

    Question and answer?


    Sorry if this question sounds retarded, but I'm not sure how to make a question and answer in Python.

    Like.. something simple such as asking ' how are you? ' if you reply ' good ' then say, ' great! '
  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2004
    Posts
    5
    Rep Power
    0
    After a chat on IRC, I've gotten a way to do it ( for the most part )

    Only problem I have is. No matter what I type it will always give me every answer in the list..

    Code:
    followups = [# ( answer, followup )
    	("cheese", "\nit's quite tasty"), 
    	("the Holy Hand Grenade", "\nit goes 'BOOM!'")]
    
    # user_answer (trigger_answer) will be answered by followups (followup)
    user_answer=raw_input('What did Francis Bacon say about:\n ')
    for trigger_answer, followup in followups:
    
    # here it is.. hope it works!
    # it doesn't!
    #	print "Bacon said about:", trigger_answer
    	print "Why that's simple, Francis Bacon said about",trigger_answer,"\b:", followup
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2003
    Posts
    53
    Rep Power
    11
    both print statements are in the for loop, so they will both be printed for each element in your list. is that the problem you're having?
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2004
    Location
    Bombay, India
    Posts
    159
    Rep Power
    13
    Hi,
    How abt using a dictionary instead of tuples!

    Subha
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2004
    Location
    Bombay, India
    Posts
    159
    Rep Power
    13
    Hi,
    So if you have no restrictions on using the dictionary then you cld do it w/o using a loop

    PHP Code:
    >>> followups={"cheese":"\nit's quite tasty","the Holy Hand Grenade":"\nit goes 'BOOM!'"}
    >>> 
    user_answer=raw_input('What did Francis Bacon say about:\n ')
    >>> 
    user_answer

    'the Holy Hand Grenade'
    >>> print "Why that's simple, Francis Bacon said about",user_answer,"\b:"followups.get(user_answer)
    Why that's simple, Francis Bacon said about the Holy Hand Grenade : 
    it goes '
    BOOM!'
    >>> 

    Subha
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2004
    Posts
    5
    Rep Power
    0
    Originally Posted by NewPythoner
    Hi,
    So if you have no restrictions on using the dictionary then you cld do it w/o using a loop :)
    Thanks a lot. :)
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2004
    Posts
    5
    Rep Power
    0
    Is there a way I can get it to say ' Nothing ' when you say something that doesn't correspond with the user_answer predefined answers?
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2004
    Location
    Bombay, India
    Posts
    159
    Rep Power
    13
    Originally Posted by KFM
    Is there a way I can get it to say ' Nothing ' when you say something that doesn't correspond with the user_answer predefined answers?
    How about 'None' instead of 'Nothing' ... if that works fine... And if you really want 'Nothing' ...then you'll have to go for some use of 'if' statement.

    PHP Code:
    >>> user_answer=raw_input('What did Francis Bacon say about:\n ')
    >>> 
    user_answer
    'DeeDee'
    >>> print "Why that's simple, Francis Bacon said about",user_answer,"\b:"followups.get(user_answer)
    Why that's simple, Francis Bacon said about DeeDee : None 

    Subha
  16. #9
  17. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2004
    Location
    Bombay, India
    Posts
    159
    Rep Power
    13
    Hi,
    There are many ways of achieving it. You cld either do it like this....

    PHP Code:
    >>> user_answer
    'DeeDee'
    >>> if str(followups.has_key(user_answer))=='False':
    ...     
    myFollow='Nothing'
    ... else:
    ...     
    myFollow=followups.get(user_answer)
    ...     
    >>> print 
    "Why that's simple, Francis Bacon said about",user_answer,"\b:",myFollow
    Why that
    's simple, Francis Bacon said about DeeDee : Nothing 

    or like this.........


    PHP Code:
    >>> if str(followups.get(user_answer))=='None':
    ...     
    myFollow='Nothing'
    ... else:
    ...     
    myFollow=followups.get(user_answer)
    ...     
    >>> print 
    "Why that's simple, Francis Bacon said about",user_answer,"\b:",myFollow
    Why that
    's simple, Francis Bacon said about DeeDee : Nothing
    >>> 

    Peace,
    Subha
  18. #10
  19. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2004
    Posts
    5
    Rep Power
    0
    Yay, I'm learning!

    Code:
    if str(followups.has_key(user_answer))=='False':
    	myFollow='nothing'
    else:
    	myFollow=followups.get(user_answer)
    
    #print "Why that's simple, Francis Bacon said about",user_answer,"\b:", myFollow
    
    if str(followups.has_key(user_answer))=='False':
    	print "Frances Bacon said",myFollow,"about", user_answer
    else:
    	print "Why that's simple, Francis Bacon said about",user_answer,"\b:", myFollow
  20. #11
  21. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    How about using the built-in get() method with it's second argument: this way you can return 'Nothing' if the key isn't found in the dictionary. Useful when you don't want to mess around with if statements .

    Code:
    >>> answers = {'Cheese': 'Love it!', 'Fish': 'Ewwwwwwww!'}
    >>>
    >>> answers.get('Cheese', 'Nothing')
    'Love it!'
    >>> answers.get('War', 'Nothing')
    'Nothing'
    >>>
    Have fun guys,

    Mark.
    programming language development: www.netytan.com Hula

  22. #12
  23. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2004
    Location
    Bombay, India
    Posts
    159
    Rep Power
    13
    Hey cool one Mark!
    Didn't know something like that existed...wow thats great!

    Subha

IMN logo majestic logo threadwatch logo seochat tools logo