2015-10-13 108 views
1

我正在運行Python的初學者指南,目前正在處理列表。現在我創建了這個示例代碼,但似乎無法將用戶輸入動態添加到我創建的列表中。如果您從列表中輸入一個項目,您會收到一條成功消息,但如果該項目不在列表中,則嘗試將其附加到當前列表中,然後返回一條錯誤消息,指出它不在清單中。最後一行是打印出來的列表,希望新增內容。我試過追加方法,甚至試圖擴展到另一個列表。有人能發現我要去哪裏嗎?將輸入附加到現有列表

topping_list_one = ['pepperoni', 'sausage', 'cheese', 'peppers'] 
error_message = "Sorry we don't have " 
success_message = "Great, we have " 

first_topping = raw_input ('Please give me a topping: ') 
if (first_topping in topping_list_one) : 
    print '{}!'.format(success_message + first_topping) 
elif (first_topping in topping_list_one): 
    topping_list_one.append('first_topping') 
else : 
    print '{}'.format(error_message + first_topping) 
print 'Heres a list of the items now in our inventory: {}'.format(topping_list_one) 
+3

你如果和你的elif具有相同的條件。只有第一個會執行。另外,如果要執行,它將添加文字'first_topping'而不是變量'first_toppping'的值。 – RobertB

回答

1

您的前兩個條件語句完全相同,根據您對目標的描述,我認爲您需要在其他流程下追加用戶輸入,如下面的代碼。

topping_list_one = ['pepperoni', 'sausage', 'cheese', 'peppers'] 
error_message = "Sorry we don't have " 
success_message = "Great, we have " 

first_topping = raw_input ('Please give me a topping: ') 
if first_topping in topping_list_one: 
    print '{}!'.format(success_message + first_topping) 
else: 
    print '{}'.format(error_message + first_topping) 
    topping_list_one.append(first_topping) 
print 'Heres a list of the items now in our inventory:{}'.format(topping_list_one) 

BTW我點你的代碼的兩個問題

  1. 你需要它在Python的方式寫,忘記了其他語言if(),蟒蛇不需要條件語句中括號(雖然加括號會不觸發語法錯誤)。

  2. 追加變量first_topping,而不是字符串first_topping,我認爲它因疏忽大意而套管:-)

+0

感謝凱文,這似乎已經做到了。我在網上發現了一些舊的Python代碼,包括(),但是我清理了。謝謝你 – RomeNYRR

+0

@RomeNYRR不客氣! :) –

2

我想你的意思是說

elif (first_topping not in topping_list_one): 
    topping_list_one.append(first_topping) 

即。 「不在」而不是「在」並從「first_topping」刪除引號

1

您的代碼不是非常一致,也有一些錯誤。你應該在那裏有兩個if條件。如果頂部位於列表中,則打印它,如果不是,則添加新的或僅顯示錯誤消息。

當然,如果你有條件不添加特殊字符以及不在列表中,你可以同時擁有這兩個字符。

添加新的澆頭:

topping_list_one = ['pepperoni', 'sausage', 'cheese', 'peppers'] 
error_message = "Sorry we don't have " 
success_message = "Great, we have " 

first_topping = raw_input ('Please give me a topping: ') 
if first_topping in topping_list_one : 
    print '{}!'.format(success_message + first_topping) 
else : 
    topping_list_one.append(first_topping) 
print 'Heres a list of the items now in our inventory: {}'.format(topping_list_one) 

錯誤消息:

topping_list_one = ['pepperoni', 'sausage', 'cheese', 'peppers'] 
error_message = "Sorry we don't have " 
success_message = "Great, we have " 

first_topping = raw_input ('Please give me a topping: ') 
if first_topping in topping_list_one : 
    print '{}!'.format(success_message + first_topping) 
else : 
    print '{}'.format(error_message + first_topping) 
print 'Heres a list of the items now in our inventory: {}'.format(topping_list_one) 

這兩種:

topping_list_one = ['pepperoni', 'sausage', 'cheese', 'peppers'] 
error_message = "Sorry we don't have " 
success_message = "Great, we have " 
char = '?=!' 

first_topping = raw_input ('Please give me a topping: ') 
if first_topping in topping_list_one : 
    print '{}!'.format(success_message + first_topping) 
elif first_topping not in topping_list_one and first_topping not in char : 
    topping_list_one.append(first_topping) 
else : 
    print '{}'.format(error_message + first_topping) 
print 'Heres a list of the items now in our inventory: {}'.format(topping_list_one) 

PS:未測試,純理論。