'How do I write plural with (s) for "Ice creams" for Chocolate, Vanilla and Strawberry if the user wants more than 1 of the 3 choices? [closed]
I want my code is display plural (s) for "Ice creams" if I have more than 1 icecream flavor. Example: Chocolate and Vanilla Ice creams. or Chocolate, Vanilla and Strawberry Ice creams.
All I get is singular and im new to Python programming after completing first chapter on Codecademy.
Here is my code below, it doesn't display Icecream nor even ice creams at the end of my flavor choices.
# First Name
company_name = "Dan's Icecream"
# Greetings
greeting_introduction = "\nHello! Welcome to "
print(greeting_introduction + company_name + "!")
first_name = input("\nCan I start off with your first name?\n\n")
print("\nThank you" + " " + first_name + "!")
# Various Icecream flavors
menu_item1 = "Chocolate\n"
menu_item2 = "Vanilla\n"
menu_item3 = "Strawberry\n\n"
order = input("\nWhat flavors would you like on your icecream today?\n\n" + menu_item1 +
menu_item2 + menu_item3)
if order == menu_item1:
print("menu_item1" + "Icecream")
if order == menu_item2:
print("menu_item2" + "Icecream")
if order == menu_item3:
print("Strawberry" + "Icecream")
if order == menu_item1 + menu_item2:
print("menu_item1" + "and" + "menu_item2" + "Ice creams")
if order == menu_item1 + menu_item3:
print("menu_item1" + "and" + "menu_item3" + "Ice creams")
if order == menu_item2 + menu_item3:
print("menu_item2" + "and" + "menu_item3" + "Ice creams")
if order == menu_item1 + menu_item2 + menu_item3:
print("menu_item1" + "menu_item2" + "and" + "menu_item3" "Ice creams")
order = input("\nYou said that you want " + order + "," + " is that right?\n\n")
Solution 1:[1]
You are getting this error because you are defining the items with "\n" like this:
menu_item1 = "Chocolate\n"
menu_item2 = "Vanilla\n"
menu_item3 = "Strawberry\n\n"
You can use .rstrip("\n") in the if so you can reformat this:
if order == (menu_item1.rstrip("\n") + menu_item2.rstrip("\n")):
print("menu_item1" + "and" + "menu_item2" + "Ice creams")
Also please note that for getting this to work the response from user should be without space:
e.g. VanillaChocolate
So you might consider that
Solution 2:[2]
Your problem here is the input. When you ask the user for input, and he/she enters a ice cream flavor, for example, chocolate, your program wants you to enter Chocolate\n which would be impossible in a terminal. So, your if statements return false.
Instead, use the code below, with comments.
# First Name
company_name = "Dan's Icecream"
# Greetings
greeting_introduction = "\nHello! Welcome to "
print(greeting_introduction + company_name + "!")
first_name = input("\nCan I start off with your first name?\n\n")
print("\nThank you" + " " + first_name + "!")
# Various Icecream flavors
menu_item1 = "Chocolate"
menu_item2 = "Vanilla"
menu_item3 = "Strawberry" #remove the newlines
order = input("\nWhat flavors would you like on your icecream today?\n\n" + menu_item1 + ', ' +
menu_item2 + ', '+ menu_item3 + '\n')
#I have indented the if statements so that this is easier to read
if order.lower() == menu_item1.lower(): # if the the order (all lowercase) is equal to the menu item (lowered) then pass the if statement
print("menu_item1" + "Icecream")
if order.lower() == menu_item2.lower():
print("menu_item2" + "Icecream")
if order.lower() == menu_item3.lower():
print("Strawberry" + "Icecream")
if order.lower() == menu_item1.lower() + "," + menu_item2.lower(): # comma-separated format for the input
print("menu_item1" + "and" + menu_item2 + "Ice creams")
if order.lower() == menu_item1.lower() + "," + menu_item3.lower():
print("menu_item1" + "and" + "menu_item3" + "Ice creams")
if order.lower() == menu_item2.lower() + "," + menu_item3.lower():
print("menu_item2" + "and" + "menu_item3" + "Ice creams")
if order.lower() == menu_item1.lower() + "," + menu_item2.lower() + "," + menu_item3.lower():
print("menu_item1" + "menu_item2" + "and" + "menu_item3" "Ice creams")
order = input("\nYou said that you want " + order + "," + " is that right?\n\n")
If this answer solved your problem, please mark as correct! Thanks...
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Alejandro Vázquez |
| Solution 2 | ottlite |
