Not sure if this is a simple question or not, pretty new to Python. Let's say I want to gather data about a user, but don't know how much info they have. How would I make it so they can keep on entering data, and when they enter a keyword like "END" the program would stop gathering data?
Asked
Active
Viewed 122 times
3 Answers
1
You can use iter
and a for loop:
for line in iter(lambda: raw_input("Enter details or "END" to quit"),"END"):
print(line)
Or use a while loop:
while True:
inp = raw_input("Enter names or end to quit")
if inp == "END":
break
# do whatever with inp

Padraic Cunningham
- 176,452
- 29
- 245
- 321
-
3
-
@JoranBeasley, me too :) especially as you can use it in a list comp too – Padraic Cunningham Feb 13 '15 at 18:48
0
Here's some sample code, if you have no idea where to begin:
user_input = [] # list of data user is entering
while True:
data = raw_input("Enter data: ")
if data.lower() == 'end':
break
else:
user_input.append(data)
print user_input

huderlem
- 251
- 1
- 5
0
One approach is you could store all the information in a list (or swap for however you want to store the info) using something like this:
data = []
while True:
user_input = raw_input("Enter something:")
if user_input == "quit":
break
data.append(user_input)
print (data)
Fancier would be building a dict
with key-value pairs for data-types and subsequent information. Doing so would follow a similar logic (or at least one approach would follow a similar logic).

HavelTheGreat
- 3,299
- 2
- 15
- 34