-3

So, instead of Python basically following the cascading script as it usually does is it possible where it will never end unless you type "end" and you can ask it any question over & over again?

I'm basically creating a bot where you basically activate it by typing 'Cofo, activate' and it will reply 'Cofo activated, how may I help you today?' and from there you can ask it things like 'What's the weather today' and 'what's 9 + 4' etc, etc. A little like Jarvis but without actual speech, more text based.

Sorry I haven't given a very good explanation, I can't really explain what I want but hopefully you understand.

  • Honestly, I have no idea. Could you give me an example? – CameronOfoluwa Jan 10 '16 at 19:00
  • 1
    Honestly, I have no idea what you're asking because your example isn't very clear. If you want to repeat something, you need a loop. If you want to input a string until you typed "end", that would be a `while` loop. To go "back to a question" implies you have somehow stored these questions in a list... – OneCricketeer Jan 10 '16 at 19:04
  • I'm basically creating a bot where you basically activate it by typing 'Cofo, activate' and it will reply 'Cofo activated, how may I help you today?' and from there you can ask it things like 'What's the weather today' and 'what's 9 + 4' etc, etc. A little like Jarvis but without actual speech, more text based. – CameronOfoluwa Jan 10 '16 at 19:09
  • Okay... If you had a language processing library that could answer any of those questions in a simple fashion, then you should understand how to write a `while` loop – OneCricketeer Jan 10 '16 at 19:13

2 Answers2

0

the way to do this with a while loop:

while True:
    user_data = raw_input('What do you want? ')
    if user_data == 'quit': break
    else:
        print 'I can\'t do "{}" yet.'.format(user_data)

All this script does is echo back your input, so ignore the details - the main thing is that the loop runs until the user enters 'quit' (or whatever word you want to specify).

Stidgeon
  • 2,673
  • 8
  • 20
  • 28
0

As noted by @cricket_007, you need a while loop. Check the tutorial for a simple introduction and the docs for a formal syntax definition.

Asking indefinitely for user input is a common pattern, be sure to check out this canonical question about a related issue: asking for user input until a valid response is provided.

Community
  • 1
  • 1
Lev Levitsky
  • 63,701
  • 20
  • 147
  • 175