0

I want to save multiple inputs. If the user of the program wants to loop it 100 times I want the program to save 100 diffrent variables like n1,n2,3 and so on.

First round:

while True:
n1 = input("What number? (done to quit) ")
if n1 == 'done':
    break

print(n1)
print(n2) and so on..

Second round:

while True:
n2 = input("What number? (done to quit) ")
if n2 == 'done':
    break

print(n1)
print(n2) and so on..

Is that possible in python3? I have googled alot and can´t find anything about it! :/

Regards

Glenn
  • 23
  • 1
  • 1
  • 3

2 Answers2

1

You can do:

results = {}
counter = 1
while True:
    response = input("What number? (done to quit) ")
    if response == 'done':
        break
    results[counter] = response
    counter += 1

You can then print the results:

print(results.items())

You can also break depending on the counter value (within the loop):

if counter == 100:
    break
Simeon Visser
  • 118,920
  • 18
  • 185
  • 180
0

Use dictionary,

i =0
d = {}
while True:
  n = input("What number? (done to quit) ")
  i += 1
  d.update({'n'+str(i): n})
  if n == 'done':
      break
print d
print d['n1'], d['n2'], d['n3']

Output:-

What number? (done to quit) 1
What number? (done to quit) 2
What number? (done to quit) 3
What number? (done to quit) 'done'
{'n1': 1, 'n2': 2, 'n3': 3, 'n4': 'done'}
1 2 3
Vishnu Upadhyay
  • 5,043
  • 1
  • 13
  • 24
  • Thanks, But I how can I use them later then? I can´t print out n2. How can I save them in the variables of n1, n2 etc? – Glenn Nov 06 '14 at 12:36