-2

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?

Bandito
  • 21
  • 2
  • So... `while data != "END":`? – Kevin Feb 13 '15 at 18:35
  • First of all im sure that if you have a search you'll find many answer ! but briefly you can use a `While` loop, for handle such tasks : `while input !='END' : #do stuff` – Mazdak Feb 13 '15 at 18:36

3 Answers3

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
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