0
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)

So far it works and I'm aware the code is not done, but instead of repeating if customerSandwichtoppings == 'wantedTopping' is there a way to let the user input every topping that is wanted to simplify the code? Also can I print the list contents without brackets and '' in a cleaner way?

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • 1
    So why not just use `print("Yes sir!\nHalf bread:", customerSandwichtoppings)`? You can use `if customerSandwichtoppings in toppings:` first to make sure it is a valid choice.. – Martijn Pieters Aug 17 '18 at 10:55
  • Welcome to Stack Overflow. Please take the [tour], read about [ask], particularly how to create a [mcve]. – Peter Wood Aug 17 '18 at 10:55

1 Answers1

0

You can simply validate the choice by testing against the list, then print the topping string itself:

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

You can combine this with the bread size; ask for both once, then combine the strings; you can uppercase the first letter of the bread choice with 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)

The while True: loops keep asking the user for the right information; if the in test is true a valid choice was made

If the user can pick more toppings, you'll have to keep a list and keep looping:

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.")

then print the list:

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

Put together as a function that'd look like:

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)
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • Yes, this is what I was looking for, but how do I allow a user to combine toppings, instead of only being able to say one at a time and rerun the code? Additionally the formatting question of how to remove brackets and '' from code when list is being printed. – generalissimo Aug 17 '18 at 11:03
  • @undergroundczar: then you'll have to make it a loop that keeps asking for any further toppings until the user says they are done. – Martijn Pieters Aug 17 '18 at 11:03
  • I made an edit on the original post, could you take a look? – generalissimo Aug 17 '18 at 11:57
  • @undergroundczar: sorry, I was away on holiday for a bit. I'll correct the error in the code, and remove the `str.format()` for now (the `{}` parts are placeholders where values to the `.format(...)` call are inserted as strings). – Martijn Pieters Aug 25 '18 at 09:14