Answer to Question #301833 in Python for Daniel

Question #301833
# 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 3: For lines 22-24, how do I include an "and" word to always be included for 2 or more flavors regardless if I respond to the input code with "Chocolate Vanilla Strawberry" or "Chocolate Vanilla and Strawberry"?


Example Output: You have selected Chocolate, Vanilla and Strawberry flavors


My Output: You have selected Chocolate, Vanilla, Strawberry flavors


1
Expert's answer
2022-02-24T04:20:07-0500
# 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)

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS