# Various Icecream flavors
menu_item1 = "Chocolate"
menu_item2 = "Vanilla"
menu_item3 = "Strawberry"
order = input("\nWhich flavors would you like on your icecream today?\n\n" +
"\n".join([menu_item1, menu_item2, menu_item3]) + "\n\n")
# Convert input to list of flavors.
order = order.lower().split()
plural = "s" if len(order) > 1 else " "
print("\nYou have selected " + ", " .join(order) + " flavor" + plural)
Question: For lines 22-24, how do I add a comma for just the first flavor when responding to the input code with choosing 3 or more flavors?
Example output: You have selected Chocolate, Vanilla and Strawberry flavors
My Output: You have selected Chocolate, Vanilla, and, Strawberry flavors
# Various Icecream flavors
menu_item1 = "Chocolate"
menu_item2 = "Vanilla"
menu_item3 = "Strawberry"
order = input("\nWhich flavors would you like on your icecream today?\n\n" +
"\n".join([menu_item1, menu_item2, menu_item3]) + "\n\n")
# Convert input to list of flavors.
order = order.lower().split()
last = order.pop();
tail = ""
if len(order):
tail = " and "
plural = "s" if len(order) > 1 else " "
print("\nYou have selected", ", ".join(order) + tail + last, "flavor" + plural)
Comments
Leave a comment