2014-01-10 122 views
2
print ('This is your Shopping List')   
firstItem = input('Enter 1st item: ')   
print (firstItem)    
secondItem = input('Enter 2nd item: ')   
print (secondItem) 

如何製作用戶所說的內容清單,然後在完成時將其打印出來?如何在Python中將用戶輸入添加到列表中

另外我該如何問他們是否已將足夠的物品添加到列表中?如果他們說不,那麼它會打印出已存儲的項目列表。

謝謝,我是新來的,所以我不知道。下面

+0

'list.append(可變)' – devnull

+5

如果你是新的,花一些時間閱讀[文件] (http://docs.python.org/)。 – devnull

回答

8
shopList = [] 
maxLengthList = 6 
while len(shopList) < maxLengthList: 
    item = input("Enter your Item to the List: ") 
    shopList.append(item) 
    print shopList 
print "That's your Shopping List" 
print shopList 
2

代碼允許用戶輸入的項目,直到它們按回車鍵即可停止:

In [1]: items=[] 
    ...: i=0 
    ...: while 1: 
    ...:  i+=1 
    ...:  item=input('Enter item %d: '%i) 
    ...:  if item=='': 
    ...:   break 
    ...:  items.append(item) 
    ...: print(items) 
    ...: 

Enter item 1: apple 

Enter item 2: pear 

Enter item 3: #press enter here 
['apple', 'pear'] 

In [2]: 
相關問題