Question: # function to select the meal pizza and salad def select_meal(): # empty list to store the selected pizza and salad list_pizza=[] list_salad=[] while True:
# function to select the meal pizza and salad def select_meal():
# empty list to store the selected pizza and salad list_pizza=[] list_salad=[] while True:
# getting user choice of pizza or salad choice=input(' Hello, would you like pizza or salad? ')
# changing the choice to lower case choice=choice.lower()
# if the choice is done, then break from the loop if choice=='done': if list_pizza or list_salad: print(" Your order has been placed. Goodbye.") break else: print(' Nothing ordered') break
# if the choice is salad, calling the salad function elif choice == 'salad':
# calling function ordered_salad= salad()
# appending the selected salad type to the list list_salad.append(ordered_salad)
# creating string to display the selected salad and pizza types s='' if list_pizza or list_salad:
s+=' You have ordered'
if list_pizza: for i in list_pizza: s+=i s+=' and ' if list_salad: for j in list_salad: s+=j s+=' and '
if s.endswith(' and '): s=s[:-5]
# displaying the created string print(s+'.',end='') con=input(" Place another order or say 'done ")
# getting choice for continuing or quitting if con=='done': if list_pizza or list_salad: print(" Your order has been placed. Goodbye.") break # if the choice is salad, calling the pizza function elif choice == 'pizza':
# calling pizza function ordered_pizza= pizza()
# appending the selected pizza to list list_pizza.append(ordered_pizza)
# creating string to display the selected salad and pizza types s='' if list_pizza or list_salad:
s+=' You have ordered '
if list_pizza: for i in list_pizza: s+=i s+=' and ' if list_salad: for j in list_salad: s+=j s+=' and '
if s.endswith(' and '): s=s[:-5]
#displaying the created string print(s+' .',end='') con=input(" Place another order or say 'done ")
# choice for continuing or quitting if con=='done': if list_pizza or list_salad: print(" Your order has been placed. Goodbye.") break # function to select the salad type def salad():
# variable to store the salad and dressing type salad_type='' dressing_type=''
# getting user input of salad salad_type=input(' Would you like a garden salad or greek salad? ')
salad_type=salad_type.lower()
# calling dressing function dressing_type=dressing() return ' a '+salad_type+' salad with '+dressing_type
def dressing():
# getting user input of dressing for salad choice=input(' please choose a dressing: vinaigrette, ranch, blue cheese, lemon ')
choice=choice.lower() return choice
def pizza():
# variable to store the pizza and toppings type pizza_type='' toppings_type=''
# getting input pizza_type=input(" Small, medium, or large? " )
pizza_type=pizza_type.lower()
# calling toppings functions toppings_type=toppings()
return ' a '+pizza_type+' pizza with '+toppings_type
def toppings(): # empty list to store the toppings t=[] while True: # getting toppings type input choice=input(" Add a topping: pepperoni, mushrooms, spinach, or say 'done ")
choice=choice.lower()
if choice == 'done': break else: t.append(choice) # returning string depends on the size of the list if len(t) == 2: return t[0]+' and ' + t[1] elif len(t) ==3: return t[0]+' and ' + t[1]+' and '+t[2] elif len(t) ==1: return t[0] else: return '' if __name__=='__main__':
select_meal()
Above is a python code for a dialog system and ordering. How can this code be rewritten so that it does not use the .endswith method, but instead it should use the .join method for strings or use string formatting?
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
