如果条件为真,如何合并列表项而不是重复

时间:2018-08-17 10:52:56

标签: python function iteration

size = 'half', 'footlong'
toppings = ['turkey', 'jalapeños', 'tomatoes', 'avocado']
sauce = ['hot sauce', 'chipotle', 'garlic sauce', 'tabasco']

def sandwich_maker(size, toppings, sauce):

    customerSandwichsize = input("What size do you want:" + str(size)+ " ")
    if customerSandwichsize == 'half':
        customerSandwichtoppings = input("OK, half a bread for you." + "\nWhat toppings to you want:" + str(toppings) + " ")
        if customerSandwichtoppings == 'turkey':
            print("Yes sir!" + "\nHalf bread: turkey")
        elif customerSandwichtoppings == 'jalapeños':
            print("Yes sir!" + "\nHalf bread: jalapeños")
        elif customerSandwichtoppings == 'tomatoes':
            print("Yes sir!" + "\nHalf bread: tomatoes")
        elif customerSandwichtoppings == 'avocado':
            print("Yes sir!" + "\nHalf bread: avocado")
    else:
        customerSandwichtoppings = input("OK, footlong bread for you." + "\nWhat toppings to you want:" + str(toppings) + " ")


sandwich_maker(size, toppings, sauce)

到目前为止,它仍然有效,并且我知道代码尚未完成,但是如果没有CustomerAandwichtoppings =='wantedTopping'重复,有没有办法让用户输入想要简化代码的每个标题?还可以以更简洁的方式打印不带括号和''的列表内容吗?

1 个答案:

答案 0 :(得分:0)

您可以通过针对列表进行测试来简单地验证选择,然后打印顶部字符串本身:

if customerSandwichtoppings in toppings:
    print("Yes sir!\nHalf bread:", customerSandwichtoppings)
else:
    print("Sorry sir, that's not a topping we have")

您可以将其与面包尺寸结合使用;要求两次,然后组合琴弦;您可以使用str.title()将面包选择的首字母大写:

while True:
    customerSandwichsize = input("What size do you want: " + str(size) + " ")
    if customerSandwichsize in size:
        break
    print("Sorry sir, that's not a size we have")

print("Okay, a", customerSandwichsize, "bread for you")

while True:
    customerSandwichtoppings = input("What toppings to you want: " + str(toppings) + " ")
    if customerSandwichtoppings in toppings:
        break
    print("Sorry sir, that's not a topping we have")

print("Yes sir!\n" + customerSandwichsize.title() + " bread:", customerSandwichtoppings)

while True:循环keep asking the user for the right information;如果in测试为真,则做出有效选择

如果用户可以选择更多的浇头,则必须保留一个列表并保持循环:

picked_toppings = []
while True:
    customerSandwichtopping = input("What toppings to you want: " + str(toppings) + " ")
    if customerSandwichtopping == 'done':
        if not picked_toppings:
            print("Sorry sir, you haven't picked any toppings yet")
            continue
        # at least one topic picked, the customer is allowed to be done,
        # break out of the while loop.
        break
    if customerSandwichtopping not in toppings:
        print("Sorry sir, that's not a topping we have")
        continue
    if customerSandwichtopping in picked_toppings:
        print("Sorry sir, you already picked that topping")
        continue
    picked_toppings.append(customerSandwichtopping)
    print("Sure thing sir. You can pick more toppings, or say 'done' when you're done.")

然后打印列表:

print("Yes sir!\n" + customerSandwichsize.title() + " bread:", picked_toppings)

将其汇总为以下功能:

def sandwich_maker(size, toppings, sauce):
    while True:
        customerSandwichsize = input("What size do you want: {} ".format(size))
        if customerSandwichsize in size:
            break
        print("Sorry sir, that's not a size we have")

    print("Okay, a", customerSandwichsize, "bread for you")

    picked_toppings = []
    while True:
        customerSandwichtopping = input("What toppings to you want: " + str(toppings) + " ")
        if customerSandwichtopping == 'done':
            if not picked_toppings:
                print("Sorry sir, you haven't picked any toppings yet")
                continue
            # at least one topic picked, the customer is allowed to be done,
            # break out of the while loop.
            break
        if customerSandwichtopping not in toppings:
            print("Sorry sir, that's not a topping we have")
            continue
        if customerSandwichtopping in picked_toppings:
            print("Sorry sir, you already picked that topping")
            continue
        picked_toppings.append(customerSandwichtopping)
        print("Sure thing sir. You can pick more toppings, or say 'done' when you're done.")

    print("Yes sir!\n" + customerSandwichsize.title() + " bread:", picked_toppings)