簡體   English   中英

Python input() 函數將用戶輸入存儲在列表或元組或字典中

[英]Python input() function to store user input in either list or tuple or dictionary

words = input("Enter a word to find its length: ")

for x in words:
    print x, len(x)

我想讓這段代碼在運行時為用戶輸入工作,以便用戶輸入可以存儲在 word 中,並且可以在運行時在 for 循環中使用。

應該怎么做?

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 

這有點 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])

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM