Hey everyone,

How would you go about creating a loop for taking user input without knowing beforehand exactly how many times it will run - as I can't seem to work out how to generate a unique name each time. So, for instance, say you want to take user input and ascribe each one a unique 'ID', like A001, A002, A003 and so on, how would you do that?

Possible psudocode to give you an idea of what I'm trying to do...

number = 001
while True:
	ID = 'A' + number
	ID = input('> ')
	if ID is 'exit':
		break
	else:
		number = number + 1

Use a list http://www.greenteapress.com/thinkpython/html/book011.html

input_list = []
while True:
	ID = input('> ')
	if ID == 'exit':     ## "is" is different from equal
		break
	else:
		input_list.append(ID)

print input_list

or a dictionary http://www.greenteapress.com/thinkpython/html/book012.html

number = 1     ## integers remove leading zeros automatically
input_dict = {}
while True:
	key = 'A%03d' % (number)
        number += 1
	ID = input('> ')
	if ID == 'exit':
		break
        ## technically, else is not required as break will exit the while loop
	else:
		input_dict[key] = ID

print "-"*70
print input_dict

print "-"*70
for key in input_dict:
    print key, "----->", input_dict[key]

Ahh yes, didn't think about using a list to do it! Thanks!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.