0
def main():
    total = 0
    print('Welcome to the Dew Drop Inn!')
    print('Please enter your choices as y or n.')
    print('     ')
    cheese = (input('Do you want a grilled cheese? '))
    dog = (input('Do you want a hot dog? ')) 
    nacho = (input('Do you want nachos? '))
    burger = (input('Do you want a hamburger? '))
    grilled_cheese = 5
    hot_dog = 5
    nachos = 4
    hamburger = 6
    cheese_burger = 1
    if cheese == 'y':
        total + grilled_cheese
    if dog == 'y':
        total + hot_dog
    if nacho == 'y':
        total + nachos
    if burger == 'y':
        total + hamburger
        the_cheese_please = input('Do you want cheese on that? ')
        if the_cheese_please == 'y':
            total + cheese_burger
    else:
        total + 0

    print('     ')
    print('The total for your food is $',(total),'.')
    tip = total * 1.15


main()

I need to be able to add up the numbers from the the user tells me. Depending on what food they want, how do I go about adding up the numbers using if/else statements? The max number of if's is 5 and the max number of else's is 1. I am very new at this so I apologize if this comes across as naive, but it would really help me out if someone gave me a tip on this. Thank you!

3
  • 1
    What exactly is wrong with the code that you posted? Commented Sep 24, 2017 at 23:53
  • You're one nice tipper, sure it shouldn't be tip = total * 0.15? Commented Sep 25, 2017 at 0:01
  • Running this code through a tool such as pylint or using a proper python ide would highlight the issue quite quickly. Commented Sep 25, 2017 at 0:04

2 Answers 2

2

total + grilled_cheese and other similar lines do nothing useful. They add the 2 numbers together, but don't do anything with the result. It would be the same as having say, a 2 just floating around in your code.

You need to reassign the result back to total:

total = total + grilled_cheese

Or more succinctly

total += grilled_cheese
Sign up to request clarification or add additional context in comments.

Comments

1

In each of your if statements you have done:

total + grilled_cheese

etc. This isn't the way you increment your value of total in python.

Instead you want to do:

total += grilled_cheese

and your else statement isn't needed; if your if statement runs then the total will increase but if it doesn't run (the input is 'n') then the total won't change.

Your code will look almost the same:

def main():
total = 0
print('Welcome to the Dew Drop Inn!')
print('Please enter your choices as y or n.')
print('     ')
cheese = (input('Do you want a grilled cheese? '))
dog = (input('Do you want a hot dog? ')) 
nacho = (input('Do you want nachos? '))
burger = (input('Do you want a hamburger? '))
grilled_cheese = 5
hot_dog = 5
nachos = 4
hamburger = 6
cheese_burger = 1
if cheese == 'y':
    total += grilled_cheese
if dog == 'y':
    total += hot_dog
if nacho == 'y':
    total += nachos
if burger == 'y':
    total += hamburger
    the_cheese_please = input('Do you want cheese on that? ')
    if the_cheese_please == 'y':
        total += cheese_burger


print('     ')
print('The total for your food is $',(total),'.')
tip = total * 1.15


main()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.