-5

What are some ways to make this code cleaner and more efficient? I'm new to python and I dont want to start bad habits! Thanks for the help!

    store = input('Name of store: ')
food = input('Type of food served: ')
serverName = 'Will'
drinkPrice = ''
foodPrice = ''
drink = input('Hello! My name is {0} and I will be your server today! What can I get you to drink?: '.format(serverName))
if drink == "Water":
    drinkPrice = 1
else :
    if drink == "Coke":
    drinkPrice = 2
else :
    if drink == "Beer":
    drinkPrice = 5
else :
    print("The item you are trying to order is not on the menu!")
drink = input("What else would you like to drink?:")
food = input('What will you be ordering tonight?: ')
if food == "Steak":
    foodPrice = 25
else :
    if food == "Pizza":
    foodPrice = 10
else :
    if food == "Salad":
    foodPrice = 5
else :
    print("The item that you are trying to order is not on the menu!")
totalPrice = str(drinkPrice) + str(foodPrice)
print('Thanks for eating at {0} and ordering {1} ! Server name: {2} Price total: {3}'.format(store, food, serverName, totalPrice))

2 Answers2

1
store = input('Name of store: ')
food = input('Type of food served: ')
serverName = 'Will'
drinkPrice = ''             #If it's a price (number why not set it to an integer example : drinkPrice = 0. '' refers to an empty string.
foodPrice = ''
drink = input('Hello! My name is {0} and I will be your server today! What can I get you to drink?: '.format(serverName))
if drink == "Water":
    drinkPrice = 1
else :                      # in python if-else are written as elif <condition>: so it would be elif drink == "Coke":
    if drink == "Coke":
    drinkPrice = 2
else :                      # will never reach here
    if drink == "Beer":
    drinkPrice = 5
else :                      #will never reach here
    print("The item you are trying to order is not on the menu!")
drink = input("What else would you like to drink?:")    # you aren't looping here so you might want a loop.
food = input('What will you be ordering tonight?: ')
if food == "Steak":
    foodPrice = 25
else :                     #same issue as the drink.
    if food == "Pizza":
    foodPrice = 10
else :
    if food == "Salad":
    foodPrice = 5
else :
    print("The item that you are trying to order is not on the menu!")
totalPrice = str(drinkPrice) + str(foodPrice)         #Python allows '+' to be used on strings as a form of concatination (combining strings). 
                                                      #You want int(drinkPrice) + int(foodPrice) or just do drinkPrice + foodPrice
                                                      #since you set those value to 1, 2, 5, etc..
print('Thanks for eating at {0} and ordering {1} ! Server name: {2} Price total: {3}'.format(store, food, serverName, totalPrice))

To sum up my comment points:

If statements are written as follow:

if <condition>:
    #do something
elif <condition>:
    #do something
else:
    #default if the top two didn't pass

You need to read up on loops but I think the one you might want is while loops:

while <condition>:
    #loops until the condition is False

The point of while loop is so you can keep asking until you get a valid answer you want. See this link for more details


Python allows + to be used on non number objects such as string:

x = "5"
y = "6"
print(x+y)
>> 56

You have to make sure your variables are numbers:

x = 5
y = 6
print(x+y)
>> 11

"5" is not the same as 5, the first is a string representation of 5, the latter is the numeric value 5. This extends to "" is an empty string not 0.

Your code doesn't work so you shouldn't be worrying about micro optimization.

This is what your code should look like:

store = input('Name of store: ')
food = input('Type of food served: ')
serverName = 'Will'
drinkPrice = 0
foodPrice = 0
drink = input('Hello! My name is {0} and I will be your server today! What can I get you to drink?: '.format(serverName))
while drinkPrice == 0:
    if drink == "Water":
        drinkPrice = 1
    elif drink == "Coke":
        drinkPrice = 2
    elif drink == "Beer":
        drinkPrice = 5
    else :
        print("The item you are trying to order is not on the menu!")
        drink = input("What else would you like to drink?:")

food = input('What will you be ordering tonight?: ')      
while foodPrice == 0:
    if food == "Steak":
        foodPrice = 25
    elif food == "Pizza":
        foodPrice = 10
    elif food == "Salad":
        foodPrice = 5
    else :
        print("The item that you are trying to order is not on the menu!")
        food = input("What else would you like to eat?:")

totalPrice = drinkPrice + foodPrice
print('Thanks for eating at {0} and ordering {1} ! Server name: {2} Price total: {3}'.format(store, food, serverName, totalPrice))
MooingRawr
  • 4,901
  • 3
  • 24
  • 31
0

why is the first line indented? You also don't need to put the server's name in a variable- it adds an unnecessary line. Just include it in the strings that the Server's name (Will) is in. You could also try placing the drinks with their prices in a dictionary. Additionally why have you done this: str(drinkPrice) + str(foodPrice) Why have you converted drinkPrice and foodPrice into strings, when they should stay as numbers? This would just join them together as strings, and would result in a logic error. So say the price of the drinks was 5 and the food price was 4, your program would make the total price into 54, when it should be 9.

  • Putting the server's name in a variable is good practice when coding at a larger scale, so you know where things are and can change it, if you need to, while they are all in one spot. Why would you recommend drinks and prices in a 2D array when there are better solutions. OP is asking for better coding practices and personally I think the last point out of your 3 is acceptable. – MooingRawr Aug 03 '17 at 16:54