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.
Asked
Active
Viewed 1.7k times
3 Answers
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
-
-
-
@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
-
-
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
-
This isn't valid Python syntax; you can't do inline assignments like that. – DSM Sep 26 '12 at 19:26
-
-
-
No, there's no real way to get it to work in a C-like way (although you could write a class to get most of the behaviour.) Incidentally, using semicolons at the end of lines isn't usual Python style. – DSM Sep 26 '12 at 19:33
-
@DSM. Yeah that I know.. Added semi-colon by mistake.. Out of habit of coding in Java.. – Rohit Jain Sep 26 '12 at 19:35