-2

I want to get user input for multiple times and store the input data together in a string until input "quit" to quit from input. I think a for loop can work but I don't know how to do it.

casterInGT
  • 9
  • 1
  • 1
  • 2

3 Answers3

6
while True:
    user_input = raw_input("Enter something:")
    if user_input == "quit":
        break
Mu Mind
  • 10,935
  • 4
  • 38
  • 69
  • castorInGT asked "store the input data together in a string", you are not doing that in your code... – mawueth Sep 26 '12 at 19:48
  • Note: In Python 3, raw_input() has been renamed to input(): http://docs.python.org/dev/py3k/whatsnew/3.0.html – jtb Nov 15 '22 at 19:28
2

Try this:

input_string = ''
while 1:
    input = raw_input('Add to string: ')
    if input == 'quit': break
    input_string += input 
mawueth
  • 2,668
  • 1
  • 14
  • 12
  • thanks! so what does "while True" or "while 1" mean? – casterInGT Sep 26 '12 at 19:31
  • @casterInGT it means loop forever – thenoviceoof Sep 26 '12 at 19:38
  • @casterInGT: as thenoviceoof said, it means loop forever, until input 'quit' breakes this infinite loop. 'While True' and 'While 1' are (almost) the same, if you want to know further details, please read this: http://stackoverflow.com/questions/3815359/while-1-vs-for-whiletrue-why-is-there-a-difference – mawueth Sep 26 '12 at 19:42
  • how do i prevent from adding "quit" to the input_string? – casterInGT Sep 26 '12 at 19:58
  • with this code, 'quit' will NOT be added to 'input_string' because the code will stop ('break') BEFORE 'quit' would get added to 'input_string' 'break' immidiately stops the loop and ignores whatever code is coming afterwards INSIDE the loop – mawueth Sep 26 '12 at 20:03
0
while True:
    input = raw_input('Prompt')
    print(input)
    if input == 'quit':
        break
Esvin Joshua
  • 71
  • 14
Rohit Jain
  • 209,639
  • 45
  • 409
  • 525