Python 2.5

This is the place for queries that don't fit in any of the other categories.

Python 2.5

Postby toastedPC » Sun Nov 24, 2013 5:12 am

Hi all, I was trying to set up an "AI" conversation that I could have with the code that I'm writing however I ran into a slightly annoying problem(keep in mind I am a very amateur coder). I was trying to get it to recognize when It was me using the program so I wrote:
Code: Select all
name = raw_input ("Hello, my name is Monty what's yours? ")
if 'Jacob' in (name):
    print "Hello creator"
print "Nice to meet you " + name + "!"

but whenever I run it, it prints both. I'm sure there is a very simple fix to this but I am just totally blanking. Any help would be appreciated, many thanks!
toastedPC
 
Posts: 6
Joined: Sat Nov 16, 2013 4:26 pm

Re: Python 2.5

Postby toastedPC » Sun Nov 24, 2013 5:21 am

so I fixed the problem but am now wondering if it would be possible to make the
Code: Select all
if 'good' in (name)
part be case insensitive so that if they typed in something like GOOD or Good etc. it would still recognize it as good
toastedPC
 
Posts: 6
Joined: Sat Nov 16, 2013 4:26 pm

Re: Python 2.5

Postby Mekire » Sun Nov 24, 2013 6:07 am

Look into str.lower() and str.upper().

Code: Select all
name = True
while name:
    name = raw_input("Enter your name: ")
    if 'good' in name.lower():
        print("Recognized good in name.")
    else:
        print("Not good.")
Code: Select all
>>>
Enter your name: gud
Not good.
Enter your name: goood
Not good.
Enter your name: Good
Recognized good in name.
Enter your name: good
Recognized good in name.
Enter your name: gOoD
Recognized good in name.
Enter your name:
Not good.
>>>

-Mek
User avatar
Mekire
 
Posts: 828
Joined: Thu Feb 07, 2013 11:33 pm
Location: Amakusa, Japan

Re: Python 2.5

Postby toastedPC » Sun Nov 24, 2013 3:21 pm

It worked! thanks a lot!
toastedPC
 
Posts: 6
Joined: Sat Nov 16, 2013 4:26 pm


Return to General Coding Help

Who is online

Users browsing this forum: tnknepp and 2 guests