将输入附加到现有列表

时间:2015-10-13 23:53:00

标签: python

我正在浏览Python的初学者指南,而我目前正在处理列表。现在我已经创建了这个示例代码,但我似乎无法将用户输入动态添加到我创建的列表中。如果您从列表中输入项目,则会收到成功消息,但如果该项目不在列表中,我会尝试将其附加到当前列表,然后我返回一条错误消息,表明它不是在库存中。最后一行只是打印出列表,希望新增加。我已经尝试过append方法,甚至尝试扩展到另一个列表。有人可以发现我出错的地方吗?

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 个答案:

答案 0 :(得分:2)

我想你的意思是说

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

即。 "不在"而不是"在"并从' first_topping'

中删除引号

答案 1 :(得分:1)

您的代码不是很一致,也有一些错误。你应该只有两个procedure TForm1.Timer3Timer(Sender: TObject);//200ms delphi timer var OriginalShots, CurrentShots: array [0..MAX_PATH] of AnsiChar; begin timer2.Enabled := false; Win32Check(ReadProcessMemory(hProc, pointer(BaseEU + AddrShotsLeft), @CurrentShots, SizeOf(CurrentShots), Read)); CurrentShots := OriginalShots;//this will ofcourse set both variables to the same value everytime, how can i do this properly? if CurrentShots < OriginalShots then begin //do stuff here UpdateOrAdd(Memo1, 13, 'Shots Left: '+string(CurrentShots)); end; end; 条件。如果顶部在列表中,则打印它,如果不是,则添加新的或仅显示错误消息。

当然,如果你创造一个不添加特殊字符的条件,那么你可以同时拥有这两个字符。

添加新的顶部:

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) 

PS:未经测试,纯粹是理论上的。

答案 2 :(得分:1)

您的前两个条件语句完全相同,并且根据您对目标的描述,我认为您需要在else流程下附加用户输入,如下面的代码。

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(),python在条件语句中不需要括号(尽管添加括号不会触发语法错误)。

  2. 追加变量first_topping而非字符串first_topping,我认为它是由粗心大意引起的: - )