Python input()函数用于在列表或元组或字典中存储用户输入

时间:2016-04-28 17:13:12

标签: python list loops for-loop input

newFile = []
with open('test.txt', 'r') as f:
    lines = f.readlines()
    for line in lines:
        newFile.append(line.strip().split(',')[1:3])

with open('test.txt', 'w') as f:
    for line in newFile:
        f.write(','.join(line) + '\n')

我希望此代码在运行时用于用户输入,以便用户输入可以存储在word中,并且可以在运行时用于for循环。

应该怎么做?

2 个答案:

答案 0 :(得分:0)

list = [None]*3
x = True

z = 0
while z < 3:
    y = raw_input("Enter a word to find its length: ")
    if(y == "n"):
        x = False;
    else:
        list[z] = y;
        print len(list[z]);
        z +=1 

答案 1 :(得分:0)

这是一个更多的Pythonic:

result = [None] * 3
for index in range(len(result)):
    y = raw_input("Enter a word to find its length: ")
    if y == "n":
        break
    else:
        result[index] = y
        print len(result[index])