2

I am trying to make a program which calculates entrance costs.. I have managed to do it except from one part which requires if two adults and three childeren then the cost is $15. Should this be done using an if statement and how would I do it?

import math 

loop = 1
choice = 0
while loop == 1:

    print "Welcome"

    print "What would you like to do?:"
    print " "
    print "1) Calculate entrance cost"

    print "2) Leave swimming centre"
    print " "

    choice = int(raw_input("Choose your option: ").strip())
    if choice == 1:
        add1 = input("Adults: ")
        add2 = input("Concessions: ")
        add3 = input("Children: ")
        print add1, "+", add2, "+", add3, "answer=", add1 *5 + add2 *3 + add3 *2   
    elif choice == 2:
        loop = 0

Thank you for any help in advance, it will be much appreciated!!

2
  • 1
    Don't use a loop variable, just use while True: then break when you want the loop to end. Commented Oct 30, 2012 at 17:41
  • Use something like if add1 == 2 and add3 == 3: print '$15'. Then put the print statement in the else: after that. Commented Oct 30, 2012 at 17:43

2 Answers 2

1

You should place an if statement for a special case, where you have the 2 adults, 3 children. Otherwise you should calculate it normally. I have commented the area where this special case occurs.

This code also assumes that the special case does not affect the price of the concessions.

import math 

loop = 1
choice = 0
while loop == 1:

    print "Welcome"

    print "What would you like to do?:"
    print " "
    print "1) Calculate entrance cost"

    print "2) Leave swimming centre"
    print " "

    choice = int(raw_input("Choose your option: ").strip())

    if choice == 1:
        add1 = input("Adults: ")
        add2 = input("Concessions: ")
        add3 = input("Children: ")

        cost = 0

        # special case for 2 adults, 3 children
        if add1 == 2 and add3 == 3:
            cost += 15
        else:
            cost += add1*5 + add3*2

        # concession cost
        cost += add2 *3

        print add1, "+", add2, "+", add3, "answer=", cost

    elif choice == 2:
        loop = 0
Sign up to request clarification or add additional context in comments.

1 Comment

OK now, although I would prefer just the part of the code you changed.
0
import math 

choice = 0
while True:

    print """
       Welcome

       What would you like to do?:

       1)Calculate entrance cost
       2) Leave swimming centre
      """

   choice = int(raw_input("Choose your option: ").strip())
   if choice == 1:
       add1 = input("Adults: ")
       add2 = input("Concessions: ")
       add3 = input("Children: ")
       print add1, "+", add2, "+", add3, "answer=", add1 *5 + add2 *3 + add3 *2   
   elif choice == 2:
       break

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.