PDA

View Full Version : I can't believe I have to ask this..



blithen
May 28th, 2008, 11:45 PM
But how do I represent the * in python?
Such as
said = str(raw_input(""))
if said == *:
print 'I like pie!'
* obviously means anything the person types in. and it will give that response also I need to know

said = str(raw_input(""))
if said == 'You like'+ * :
print 'I like pie!'
meaning if the person typed in You like then anything after that it would output that response.

lapubell
May 28th, 2008, 11:52 PM
i'm no expert, but i think all you have to do is


if said:

because that will return a true if it is set, false if not...

blithen
May 28th, 2008, 11:54 PM
i'm no expert, but i think all you have to do is


if said:

because that will return a true if it is set, false if not...

Hm..well that answers the first question thank you!

bobbocanfly
May 28th, 2008, 11:55 PM
Not sure why you need this but this is how i would implement your script.



said = ""
while len(said) < 1: #Makes sure they type something in
said = str(raw_input(""))
print "You said " + said


Edit: The second part looks like you want the user to type something starting with "I like", correct? You can use the startswith() function:



if said.startswith("I like"):
print "WIN"

amingv
May 28th, 2008, 11:58 PM
I don't understand why make a condition if you're going to evaluate against "everything"; might as well just do 'print said'.
For the second one you can do:

if said.startswith('You like'):
#do something

Also there's no need to do str(raw_input()); raw_input returns a string by default (even if you input numbers).
EDIT: Dang he beat me to it.

blithen
May 29th, 2008, 12:02 AM
Not sure why you need this but this is how i would implement your script.



said = ""
while len(said) < 1: #Makes sure they type something in
said = str(raw_input(""))
print "You said " + said


Edit: The second part looks like you want the user to type something starting with "I like", correct? You can use the startswith() function:



if said.startswith("I like"):
print "WIN"

SWEET. I love you! Thanks man xD

Lau_of_DK
May 29th, 2008, 12:17 AM
I recommend that you read: this thread (http://ubuntuforums.org/showthread.php?t=803185)

...start to finish, it covers questions similar to what you're asking.

/Lau

Can+~
May 29th, 2008, 12:36 AM
I also have a suggestion:
http://docs.python.org/lib/re-syntax.html

When the conventional methods (split, startswith, endswith..) just don't cut it.

blithen
May 29th, 2008, 03:46 AM
One more question similar to the one I asked.

feel = str(raw_input('Hello, I am AIS. \nHow are you? '))
if feel == *'horrible'*:
print 'that sucks'

how would I do that. I get .endswith() and .startswith()
but is there like a .hasin('weeee')??
EDIT: or what I use the 're' module?

Can+~
May 29th, 2008, 04:32 AM
feel.find("horrible") != -1

http://docs.python.org/lib/string-methods.html

With Regular expressions (which I don't master):


import re

feel = "I feel horrible man."

if re.compile(".*?(horrible|terrible|awful).*?", re.I).match(feel, 1):
print "Sorry dude."
else:
print "uhm."

. being any character
* being 0 or more repetitions of the last character (in this case, any character)
? to avoid greedy.

I think .find is better in this one, regexp is better for other more complex tasks.

slavik
May 29th, 2008, 04:39 AM
comming from a perl background, I would recommend that you look into regular expressions.

ghostdog74
May 29th, 2008, 04:47 AM
One more question similar to the one I asked.

feel = str(raw_input('Hello, I am AIS. \nHow are you? '))
if feel == *'horrible'*:
print 'that sucks'

how would I do that. I get .endswith() and .startswith()
but is there like a .hasin('weeee')??
EDIT: or what I use the 're' module?



if "horibble" in feel:
...

Can+~
May 29th, 2008, 05:01 AM
if "horibble" in feel:
...


Oh.. right. I forgot about that one.

Anyway, (to the OP), are you using multiple if's for each word? Like "if terrible" "if horrible" "if awful" ... ? That could be easier with regexps.

blithen
May 29th, 2008, 05:20 AM
Oh.. right. I forgot about that one.

Anyway, (to the OP), are you using multiple if's for each word? Like "if terrible" "if horrible" "if awful" ... ? That could be easier with regexps.
o.O Really? How would I use that?

LaRoza
May 29th, 2008, 05:26 AM
o.O Really? How would I use that?

http://www.amk.ca/python/howto/regex/

ghostdog74
May 29th, 2008, 06:08 AM
Anyway, (to the OP), are you using multiple if's for each word? Like "if terrible" "if horrible" "if awful" ... ? That could be easier with regexps.

before going into regexps, it might be worthwhile to explore


if ..... in ["horibble","awful","blah"]

or


for items in ["horrible","awful","blah"]:
if items in word:
.....