我想保存多个输入。如果程序的用户想要循环100次,我希望程序保存100个不同的变量,如n1,n2,3等等。
第一轮:
while True:
n1 = input("What number? (done to quit) ")
if n1 == 'done':
break
print(n1)
print(n2) and so on..
第二轮:
while True:
n2 = input("What number? (done to quit) ")
if n2 == 'done':
break
print(n1)
print(n2) and so on..
在python3中可以吗?我用谷歌搜索了很多,但却找不到任何关于它的东西! :/
此致
答案 0 :(得分:0)
你可以这样做:
results = {}
counter = 1
while True:
response = input("What number? (done to quit) ")
if response == 'done':
break
results[counter] = response
counter += 1
然后您可以打印结果:
print(results.items())
您也可以根据计数器值(在循环内)中断:
if counter == 100:
break
答案 1 :(得分:0)
使用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']
输出: -
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