2

入場料を計算するプログラムを作ろうとしています。大人2人と子供3人の場合、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

大人 2 人、子供 3 人という特別な場合には、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: ")

        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 に答える