我对Python相当陌生,周末一直在做一些练习。我现在正忙着做一个电影订票计划。在
我一直在研究如何给特定年龄段的百分比值,我在代码中留下了一些注释,我需要包括一些how。在
标准票价14.95折,需要打9折和15折。在
我需要使用一个名为“buyTicket()”的函数,它包含在计算百分比步骤的参数中。在
目前,该计划能够确定年龄,但我不确定如何增加一定年龄的百分比,然后总票价后,每票已选定。在
下面是我当前的代码。任何建议,包括和使用是非常感谢!在
谢谢。在
def cinema():
age = int(input("Please enter your age: "))
if age < 18:
print("Sorry. You are not over 18 years old. You cannot watch this movie.")
if age >= 18:
print("You are allowed to see the film. Standard ticket price is £14.95 ")
#twentydiscount = 10% (over 20)
#oapdiscount = 15% (over 65)
def buyTicket(percentageDiscount):
if age <= 20:
print("You are under 20 years old. You are eligible for a 10% discount. Your ticket price will be", percentageDiscount)
elif age >= 65:
print("You are 65 years or over. You are eligible for a 15% discount. Your ticket price will be", percentageDiscount)
else:
print("You are", age, "years old. Your ticket price will be £14.95")
# totalticketprice = (adding up prices of tickets after tickets have been selected each time)
while (True):
cinema()
anotherticket = input("Do you want to find out more information about another ticket? (Yes/No): ")
if anotherticket == 'No':
exit()
buyTicket(percentageDiscount)
首先定义一个函数来计算数字的百分比。你可以多次使用内联函数,但你可以这样做:
然后,您可以在需要的地方调用此函数,如下所示:
^{pr2}$您还必须创建价格变量。在
附言:你的问题闻起来像家庭作业;)
如果我读对了,我想这就是你想要的:
我认为这是对的,你要做的前3行是计算,从一个数字得到一个百分比。在
尽量不要混淆逻辑和接口(为用户打印)太多。首先要注意你的逻辑:
以下是您的脚本示例(包括简短的句子….):
典型输出:
^{pr2}$相关问题 更多 >
编程相关推荐