我正在尝试制作一个计算入场费的程序。我已经设法做到了,除了一个部分,如果有两个成人和三个 child ,那么费用是 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
提前感谢您的任何帮助,我们将不胜感激!!
请您参考如下方法:
您应该为特殊情况放置一个 if 语句,其中有 2 名成人和 3 名 child 。否则你应该正常计算它。我已经评论了发生这种特殊情况的区域。
此代码还假定特殊情况不会影响优惠价格。
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
