Question on List

S

subhabrata.iisc

Dear All,
I am trying to write the following code:

def try1(n):
a1="God Godess Borother Sister Family"
a2=a1.split()
a3=raw_input("PRINT A WORD")
a4=a1.find(a3)
print a4
a5=[]
if a4>0:
a5=a2.index(a3)
a6=a5+1
a7=a2[a6]
print "The new word is"
print a7
a8=a5.append(a7)
print a5
elif a4<0:
a11=a3
print "The word is not availiable in String"
print a11
a6=a5.append(a11)
print a5
else:
print "Error"

Now, my question is I like to see a5 or the empty list as appended
with elements. Results I am getting is a5 giving single value like
['God'],['Godess']... but I like to see it as ['God','Godess'...] etc.
Am I going wrong?
Do I have to rethink it in some other way?
If any one can kindly let me know.
Best Regards,
Subhabrata.
 
C

Chris

Dear All,
I am trying to write the following code:

def try1(n):
        a1="God Godess Borother Sister Family"
        a2=a1.split()
        a3=raw_input("PRINT A WORD")
        a4=a1.find(a3)
        print a4
        a5=[]
        if a4>0:
                a5=a2.index(a3)
                a6=a5+1
                a7=a2[a6]
                print "The new word is"
                print a7
                a8=a5.append(a7)
                print a5
        elif a4<0:
                a11=a3
                print "The word is not availiable in String"
                print a11
                a6=a5.append(a11)
                print a5
        else:
                print "Error"

Now, my question is I like to see a5 or the empty list as appended
with elements. Results I am getting is a5 giving single value like
['God'],['Godess']... but I like to see it as ['God','Godess'...] etc.
Am I going wrong?
Do I have to rethink it in some other way?
If any one can kindly let me know.
Best Regards,
Subhabrata.

First notes, the string .find() method return -1 for not found and
zero or above if the search string is present. Remember you count
from zero. Secondly, list .append() methods do not return a new list
but modify the list in place. Thirdly, the .index() method of a list
requires an integer and not a string. And lastly, indentation should
be 4 spaces not 8.

Just doing a sloppy job on the code (and my interpretation of what you
wanted)

def try1(n):
new_list = []
input_string="God Godess Borother Sister Family"
while n:
user_selection=raw_input("PRINT A WORD")
if input_string.find(user_selection) > -1:
s = input_string.split()
i = [i for i,j in enumerate(s) if j == user_selection]
new_word = s[i+1]
print 'The new word is %s' % new_word
new_list.append(new_word)
print new_list
else:
print 'User selection of "%s" not in the string.' %
user_selection
new_list.append(user_selection)
print new_list
n -= 1

Obviously I'm going to assume that the code you posted excluded your
loop control for the "try n amount of times". What was most likely
the cause is that you loop through your structure for every attempt of
the n times but each time you reset your list when you re-create it.

Hope that helps some.
Chris
 
S

subhabrata.iisc

Hi Chris,
I solved the problem some other way round but thanx for your
suggestion, I'll review it also.
Best Regards,
Subhabrata.
Dear All,
I am trying to write the following code:

def try1(n):
� � � � a1="God Godess Borother Sister Family"
� � � � a2=a1.split()
� � � � a3=raw_input("PRINT A WORD")
� � � � a4=a1.find(a3)
� � � � print a4
� � � � a5=[]
� � � � if a4>0:
� � � � � � � � a5=a2.index(a3)
� � � � � � � � a6=a5+1
� � � � � � � � a7=a2[a6]
� � � � � � � � print "The new word is"
� � � � � � � � print a7
� � � � � � � � a8=a5.append(a7)
� � � � � � � � print a5
� � � � elif a4<0:
� � � � � � � � a11=a3
� � � � � � � � print "The word is not availiable in String"
� � � � � � � � print a11
� � � � � � � � a6=a5.append(a11)
� � � � � � � � print a5
� � � � else:
� � � � � � � � print "Error"

Now, my question is I like to see a5 or the empty list as appended
with elements. Results I am getting is a5 giving single value like
['God'],['Godess']... but I like to see it as ['God','Godess'...] etc.
Am I going wrong?
Do I have to rethink it in some other way?
If any one can kindly let me know.
Best Regards,
Subhabrata.

First notes, the string .find() method return -1 for not found and
zero or above if the search string is present. Remember you count
from zero. Secondly, list .append() methods do not return a new list
but modify the list in place. Thirdly, the .index() method of a list
requires an integer and not a string. And lastly, indentation should
be 4 spaces not 8.

Just doing a sloppy job on the code (and my interpretation of what you
wanted)

def try1(n):
new_list = []
input_string="God Godess Borother Sister Family"
while n:
user_selection=raw_input("PRINT A WORD")
if input_string.find(user_selection) > -1:
s = input_string.split()
i = [i for i,j in enumerate(s) if j == user_selection]
new_word = s[i+1]
print 'The new word is %s' % new_word
new_list.append(new_word)
print new_list
else:
print 'User selection of "%s" not in the string.' %
user_selection
new_list.append(user_selection)
print new_list
n -= 1

Obviously I'm going to assume that the code you posted excluded your
loop control for the "try n amount of times". What was most likely
the cause is that you loop through your structure for every attempt of
the n times but each time you reset your list when you re-create it.

Hope that helps some.
Chris
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top