Member Avatar for sravan953

Hey guys,

I wanted to make a program which keeps asking you questions, and continues on and on based on your answers...

So basically, I wanted to know whether the user has typed 'why' in the first input, so I tried:

s=raw_input("Are you feeling bored?\n")
s1=[s]

a=0

for x in s:
    if(x=='w'):
        print s1[a:a+2]
    a+=1

So basically, if I enter 'he hey heya why' it should print 'why'..but it doesn't! Can anyone help me?

Look through your code and test print some of your variables to see what is going on.

Create a word list then something like this will work:

s = 'he said why'
word_list = s.split()
n = 0
for word in word_list:
    if word[0] == 'w':
        print word_list[n]
    n += 1

From a first glance it looks like the problem is: print s1[a:a+2] which should be print s1[0][a:a+2] because s1 is a list with only one element (the string s), and you want to output the range of characters in the string not the range of elements in the list.

You might also consider sneekla's code, but instead of using: if word[0] == 'w': you could use if word == 'why': so it will only output on 'why' instead of every word that starts with 'w'

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.