2

我正在尝试制作一个计算入场费用的程序。我已经设法做到了,除了一个需要两个成人和三个孩子的部分,那么费用是 15 美元。这是否应该使用 if 语句来完成,我该怎么做?

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

提前感谢您的任何帮助,我们将不胜感激!!

4

2 回答 2

1

您应该为特殊情况放置一个 if 语句,即您有 2 个成人,3 个孩子。否则你应该正常计算。我已经评论了发生这种特殊情况的区域。

此代码还假设特殊情况不影响优惠的价格。

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
于 2012-10-30T17:48:15.927 回答
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
于 2012-12-07T01:19:11.780 回答