Click to See Complete Forum and Search --> : Python - list item problem


George Kilroy
12-10-2001, 09:09 PM
#!/~/~/env python

mylist = ["bob","bill"]

userinput = raw_input("type here:")
if userinput == "hello "+mylist[???]:
print "Hi, you said "+mylist[???]
else:
print "whatever."


This is a simplified version of what my problem looks like. Where the first question marks are I want to make this equal to either "bob" or "bill" but where the second set of question marks are, I want this equal to only one of the two, the one that the user typed. I have tried


n = 0 or 1
...
if userinput == "hello "+mylist[n]:
...


but then I still couldn't get the second "???" to show up as the one that the user typed.
Thanks.

kmj
12-10-2001, 10:01 PM
well, to start with,

n = 0 or 1

sets n to the value 1...

so, I don't think that's exactly what you want to be doing...

To find out which string to use, you're going to have to do one of two things:
1) use a regular expression to find the name in the string
2) seperate the user input into its words, and either look at all the words for the name and find the specified name...

George Kilroy
12-11-2001, 12:17 AM
I'm a newbie to Python so if this is something that is too difficult for a newbie tell me and I'll try to find some other problem to focus on. I'm waiting till after Christmas to get any books.

<STRONG>1) use a regular expression to find the name in the string</STRONG>

Well, I found keep finding stupid tutorials on this obviously made for people who can already program. :rolleyes: I didn't find any beginners's tutorial so it looks like I need to know a ton of other things before I could tackle this.

<STRONG>
2) seperate the user input into its words, and either look at all the words for the name and find the specified name...</STRONG>

I don't really follow. You aren't meaning for me to
...
if userinput == "hello "+mylist[0]:
print "Hi, you said "+mylist[0]
elif userinput == "hello "+mylist[1]:
print "Hi, you said "+mylist[1]
...

because that would take too long. It will be a long list, and there will be other lists that use similar things. Or did you not mean that at all?

kmj
12-11-2001, 10:48 AM
I actually kind of figured my reply was a bit advanced, but I wasn't able to go more in depth last night; sorry.

Don't worry about regular expressions for now... okay, basically the point is taht to be able to determine which name the user entered, you have to actually look at the string they entered, right?

Assuming they are guaranteed to enter something that fits the format you're using above, then the following code should work:


name = userinput.split()[1]
if name == mylist[1] or name == mylist[2]:
print "Hi, you said " + name
else
print "whatever"

# or something along those lines...
# to make that first line a little clearer,
# you could do:

words = userinput.split()
name = words[1]

# that splits the string userinput into
# seperate words and then takes the second
# word and puts it in the variable 'name'.


For more info on the split() function, and other string functions, here's the page in the python library reference:
http://python.org/doc/current/lib/module-string.html

kmj
12-11-2001, 10:58 AM
"because that would take too long. It will be a long list, and there will be other lists that use similar things. Or did you not mean that at all?"

Okay, so you're going to have a long list of words, and you want to see if word 'x' matches any of the words in a given list? This is a good way to do that:


count = 0
for i in y:
if x == i:
break
count += 1

if count == len(y):
print "not found!"
else:
print "found!"


I'm using a for loop to compare each element in the array to x to see if there is a match.. If there is a match, you break out of the loop (note: don't do this for school assignments, most teachers won't allow it) and count will be the index of the match. If there are no matches, the value of count will end up being equal to the total length of the list, and you can check this value after the loop to see if you've found a match or not. Oh, and if you really want to access the list to print out the user name, instead of just printing out the variable you obtained from splitting the string that the user entered, you can use mylist[count].

Hope that clarifies things abit... if you still have questions, please ask. :D

Strike
12-11-2001, 04:29 PM
Wouldn't a faster solution be to use all the acceptable inputs as keys in a hash table, and then simply using list.has_key(foo) to check instead of iterating through the list (though that may be what has_key() does anyway)?

binaryDigit
12-11-2001, 05:09 PM
why not just use something like this?

#!/usr/bin/env python

names = ["bart","lisa","homer","maggie","marge"]

a_name = "homer"
print a_name
if a_name in names:
print "yup its in there"
else:
print "nope can't find it"

a_name = "ned flanders"
print a_name
if a_name in names:
print "yup its in there"
else:
print "nope can't find it"

George Kilroy
12-11-2001, 07:07 PM
Thanks very much. I think I can get it to work now. :)

George Kilroy
12-11-2001, 08:02 PM
Hmmm....the split thing doesn't work that way for me because there are many other things that the user may type not in the format of what I showed. However, I think it is what I need somewhere else. I'll add to the sorce and point out what else I think I need to solve this.

mylist = ["bob","bill","ben",...]
any = ?

user_input = raw_input("")
if user_input == "blahblahblah":
print "whatever."
if user_input == "hello "+mylist[any]:
name = user_input.split()[1]
print "Hi, you said "+mylist[name]
name = Null
else:
print "sure, whatever."

So on line 2, how how do I make 'any' equal to 0, 1, and 2?

This whole thing is inside a while loop, so on line 9 I sent 'name' equal to Null. Is that even how I give it null value? I want 'name' to no longer exist. That way I won't get bugs when someone types just plain "blahblahblah."

[ 11 December 2001: Message edited by: George Kilroy ]

George Kilroy
12-11-2001, 08:11 PM
BTW, on line 8, the format of it saying mylist[name] cannot be changed and 'name' must be told to be equal to a number. (ex. 0, but not 'bob')

kmj
12-11-2001, 09:32 PM
Strike - yes; I hadn't thought of that.

binaryDigit - Sure, if that syntax works. I'm not completely familiar with python.

George - You cause a single variable to have multiple values, as far as I know. Also, if the string that the user enters may vary (i.e. the name is not always the second word, and there may be a different number of words) then how do you expect to know which word is the name?

this line: "if user input == "hello "+mylist[any]" is just never going to work for multiple keys. Also, if your input is going to be different, then why do you have "hello " hard coded in?

I guess I'm not sure what you're really trying to do in your program (as opposed to the code you typed here), and I think you're trying to make python do things that it can't do... at least not the way you're trying to do them.

binaryDigit
12-12-2001, 05:18 AM
ok i got it
#!/usr/bin/env python

mylist = ["bob", "bill"]

userinput = raw_input("type here:")

stuff = userinput.split()

for item in range(len(stuff)):
if stuff[item] in mylist:
print 'hi, you said ' + stuff[item]
break
else:
print 'whatever'


:D

binaryDigit
12-12-2001, 08:20 AM
any even cleaner way

#!/usr/bin/env python

mylist = ["bob", "bill"]

userinput = raw_input("type here:")

stuff = userinput.split()

for item in stuff:
if item in mylist:
print 'hi, you said ' + item
break
else:
print 'whatever'

:D :D

[ 12 December 2001: Message edited by: binaryDigit ]

George Kilroy
12-12-2001, 08:09 PM
Thanks very much.

Strike
12-12-2001, 08:26 PM
Cool, I didn't know lists had builtin "in" functionality. That's pretty neat :) Thanks binaryDigit. That basically is why I wanted to use hashes instead (since I knew those had has_key()).