0

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

Edited by Luxifour: n/a

2
Contributors
2
Replies
4
Views
5 Years
Discussion Span
Last Post by Luxifour
0

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]

Edited by woooee: n/a

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.