Python中的订票程序

2024-06-17 10:37:21 发布

您现在位置:Python中文网/ 问答频道 /正文

我对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)

Tags: youageyourifticketwillpriceold
3条回答

首先定义一个函数来计算数字的百分比。你可以多次使用内联函数,但你可以这样做:

def percentage(price, pct):
    return (pct * price) /100

然后,您可以在需要的地方调用此函数,如下所示:

^{pr2}$

您还必须创建价格变量。在

附言:你的问题闻起来像家庭作业;)

如果我读对了,我想这就是你想要的:

n = £14.95
x = n / 100
percent = n * x - 100
price = abs(age * percent / 100)

我认为这是对的,你要做的前3行是计算,从一个数字得到一个百分比。在

尽量不要混淆逻辑和接口(为用户打印)太多。首先要注意你的逻辑:

  • 将原始价格存储在变量中并保持不变
  • 根据买家的年龄,将折扣存储在一个变量中
  • 收集所有你需要的信息
  • 最后计算
  • 必要时通知用户

以下是您的脚本示例(包括简短的句子….):

#!/usr/bin/python3


def cinema():
    age = int(input('Enter age: '))

    if age < 18:
        print('  Too young for this movie.')
        return

    discount = get_discount(age)
    print_discount_message(discount)

    price = 14.95
    discd = calculate_discount_price(price, discount)

    print(f'  Your price: {discd} (original price: {price})')


def get_discount(age):
    if age <= 20:
        discount = 0.1
    elif age >= 65:
        discount = 0.15
    else:
        discount = 0.0

    return discount


def print_discount_message(discount):
    if discount == 0.0:
        print('  Not qualified for discount.')
    else:
        print('  Qualified for discount: {}%'.format(int(discount * 100)))


def calculate_discount_price(original_price, discount):
    return round(original_price - original_price * discount, 2)


if __name__ == '__main__':
    while True:
        cinema()
        more = input('Buy more? (Yes/No): ')
        if more != 'Yes':
            break

典型输出:

^{pr2}$

相关问题 更多 >