您的代碼不是非常一致,也有一些錯誤。你應該在那裏有兩個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:未測試,純理論。
你如果和你的elif具有相同的條件。只有第一個會執行。另外,如果要執行,它將添加文字'first_topping'而不是變量'first_toppping'的值。 – RobertB