Python IF查询
我想做一个计算入学费用的程序。。我已经设法做到了,除了一部分需要两个成年人和三个孩子,那么费用是15美元。这是否应该使用if语句来完成,我将如何完成Python IF查询,python,if-statement,Python,If Statement,我想做一个计算入学费用的程序。。我已经设法做到了,除了一部分需要两个成年人和三个孩子,那么费用是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" prin
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语句。否则你应该正常计算。我已经评论了发生这种特殊情况的领域 本准则还假设特殊情况不影响优惠价格
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
不要使用循环变量,只需使用
而True:
然后在希望循环结束时使用中断
。如果add1==2和add3==3,则使用类似的方法:打印“$15”
。然后将print语句放在else:
之后。现在可以了,不过我更喜欢您更改的代码部分。
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