Page 1 of 2 12 LastLast
Results 1 to 10 of 18

Thread: Another newbie Python question

  1. #1
    Join Date
    Apr 2008
    Beans
    11

    Another newbie Python question

    Thanks for taking the time to have a look!

    I'm having some strange output from this script:

    Code:
    import random
    
    responses = {"hi":["Hello.", "Howdy!", "Whussup?"],
    	     "hello":["Hey!", "Hi there!", "How are you?"]}
    			
    user_says = raw_input("Please enter text to chat.\nEnter \"quit\" at any time to exit.\n: ")
    
    while user_says != "quit":
    	user_says = user_says.lower()
    	user_list = user_says.split()
    	format_list = []
    	for word in user_list:
    		if word[-1].isalpha() == False:
    			format_list.append(word[0:-1])
    		else:
    			format_list.append(word)
    	for word in format_list:
    		if word in responses:
    			user_says = raw_input(random.choice(responses[word]) + "\n: ")
    		else:
    			user_says = raw_input("Sorry, I don't understand.\n: ")
    			
    print "It's been nice talking to you."

    It seems that at random intervals, the script fails to recognise a word which is in the responses dictionary. Any pointers as to what I'm doing wrong would be appreciated. In addition, any time a word present in responses is entered with punctuation, the script fails to recognise it. Would there be a better way of removing nonalpha characters?


    Sample output:

    Code:
    Please enter text to chat.
    Enter "quit" at any time to exit.
    : Hi there.
    Hello.
    : Hi there.
    Sorry, I don't understand.
    : Hello
    How are you?
    : Hello
    Hey!
    : Hello there.
    Hey!
    : Hello, how are you?
    Sorry, I don't understand.
    :
    Thanks in advance for your help.

  2. #2
    Join Date
    May 2008
    Location
    Cleveland, Ohio
    Beans
    68
    Distro
    Ubuntu Karmic Koala (testing)

    Re: Another newbie Python question

    To debug something like this, i'd recommend printing out at each step the variables your referring to, i.e. word, word[-1], word[0:-1] etc. to see exactly what is being evaluated in your script.

    Good debugging techniques are useful for getting stuff to work like this

  3. #3
    Join Date
    Jun 2008
    Location
    Narnia
    Beans
    784
    Distro
    Ubuntu 9.04 Jaunty Jackalope

    Re: Another newbie Python question

    Quote Originally Posted by digitalvectorz View Post
    To debug something like this, i'd recommend printing out at each step the variables your referring to, i.e. word, word[-1], word[0:-1] etc. to see exactly what is being evaluated in your script.

    Good debugging techniques are useful for getting stuff to work like this
    I've found that it's sometimes helpful using the interactive shell to accomplish this.
    Blog
    #!
    Chan eil mi a Gàidhlig agam...pero si hablo español: soy catracho.
    Proud to be a Browncoat!

  4. #4
    Join Date
    Mar 2005
    Location
    Haarlem, The Netherlands
    Beans
    363

    Re: Another newbie Python question

    I also would add
    Code:
    for i in responses:
    	print i
    right after you define dictionary responses to help you see why something else happens than what you probably expect to happen

  5. #5
    Join Date
    Jun 2006
    Location
    Gwangju, Korea
    Beans
    3,479

    Re: Another newbie Python question

    Quote Originally Posted by Martin Witte View Post
    Code:
    for i in responses:
        print i
    Or just
    Code:
    print responses

  6. #6
    Join Date
    Mar 2005
    Location
    Haarlem, The Netherlands
    Beans
    363

    Re: Another newbie Python question

    Quote Originally Posted by mssever View Post
    Or just
    Code:
    print responses
    This is a differenent approach, the command I suggested prints the keys of the dictionary, where you print the whole dictionary.

  7. #7
    Join Date
    Jun 2006
    Location
    CT, USA
    Beans
    5,267
    Distro
    Ubuntu 6.10 Edgy

    Re: Another newbie Python question

    pretty-print : http://docs.python.org/library/pprint.html is excellent for debug prints - even of whole structures. Batteries included!

  8. #8
    Join Date
    Dec 2008
    Beans
    Hidden!

    We can give you the answer directly...

    but it would be no fun there for you. I think you will learn much more discovering what's going on by yourself -and you will have much more fun too!!

    So here it is you code with just two strategically placed print orders:

    Code:
    import random
    
    responses = {"hi":["Hello.", "Howdy!", "Whussup?"],
    	     "hello":["Hey!", "Hi there!", "How are you?"]}
    			
    user_says = raw_input("Fuerza! Please enter text to chat.\nEnter \"quit\" at any time to exit.\n: ")
    
    
    while user_says != "quit":
    	user_says = user_says.lower()
    	user_list = user_says.split()
    	format_list = []
    	for word in user_list:
    		if word[-1].isalpha() == False:
    			format_list.append(word[0:-1])
    		else:
    			format_list.append(word)
    	print 'format_list is:',format_list
    	for word in format_list:
    		print 'word    out of   user_list:',word
    		if word in responses:
    			user_says = raw_input(random.choice(responses[word]) + "\n: ")
    		else:
    			user_says = raw_input("Sorry, I don't understand.\n: ")
    			
    print "It's been nice talking to you."

    Go and run it, using your input that worked at random:
    First input ---> Hi there.
    Second input --> Hi there.
    You will spot the problem easily, i guess.

    Programming is fun!!!! Learning too!!!!

  9. #9
    Join Date
    Apr 2007
    Location
    (X,Y,Z) = (0,0,0)
    Beans
    3,715

    Re: Another newbie Python question

    There's a logic problem in your program, consequence of bad design. Hint: try printing what words are being analyzed each time and never mix implementation and interface

  10. #10
    Join Date
    Apr 2008
    Beans
    11

    Re: Another newbie Python question

    Hey folks, thanks for your responses and your patience with a newcomer!

    I think I've pretty much got it. Kicked myself when I saw what I'd done. Please correct me if I'm wrong, but:

    When the user entered a string, for example, "Hi, how are you" the for loop would proceed to proceed to user_formatted[0] (i.e. "hi") and I'd get a positive result. If the user then entered "Hello." the word checked would actually be "how" - right?

    I now have:

    Code:
    import random
    
    responses = {"hi":["Hello.", "Howdy!", "Whussup?"],
    	     "hello":["Hey!", "Hi there!", "How are you?"]}
    			
    user_says = raw_input("Please enter text to chat.\nEnter \"quit\" at any time to exit.\n: ")
    
    while user_says != "quit":
    	user_says = user_says.lower()
    	user_list = user_says.split()
    	format_list = []
    	for word in user_list:
    		print word[-1], "- Last char in word."
    		if word[-1].isalpha() == False:
    			format_list.append(word[0:-1])
    			print format_list, " - The current list"
    		else:
    			format_list.append(word)
    	for word in format_list:
    		if word in responses:
    			user_says = raw_input(random.choice(responses[word]) + "\n: ")
    		else:
    			user_says = raw_input("Sorry, I don't understand.\n: ")
    			
    print "It's been nice talking to you."
    Any better?

Page 1 of 2 12 LastLast

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •