【问题标题】:i got set this coding task from my school and am having trouble doing the python code我从学校得到了这个编码任务,但在编写 python 代码时遇到了麻烦
【发布时间】:2021-01-27 11:52:49
【问题描述】:

https://www.101computing.net/entry-fees-calculator-using-a-flowchart/

我正在尝试完成学校发给我的编码挑战(上面附有链接),但我陷入了困境,所以我希望有人能提供帮助。我在尝试添加折扣计算部分时被卡住了,因为我必须查看价格是否超过 50,如果是,则对其应用 5% 的折扣

def ThemePark():
    age = int(input('Enter age: '))
    if age <= 15:
        print("Your entry price is £11")
    if age >= 15:
        print("Your entry price is £13.50")
    if age >= 18:
        print("Your entry price is £15 ")
    discount = get_discount(price1 + price2 + price3)
    print_discount_message(discount)
    price > 50
    price1 = 11
    price2 = 13.50
    price3 = 15
    price > 50
    discountprice = calculate_discount_price(price, discount)
    print(f'  Your price: {discd} (original price: {price})')

def Discount(price):
    if price > 50:
        discount = 0.95
    else:
        discount = 0.0

    return discount

def print_discount_message(discount):
    if discount == 0.0:
        print('  Not qualified for family 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:
        ThemePark()
        more = input('Buy more? (Yes/No): ')
        if more != 'Yes':
            break

【问题讨论】:

  • 你希望像price &gt; 50这样的代码应该单独做什么?
  • 当您有更多经验时,您会发现为什么使用浮点数(其中包含小数点的数字)有时无法正常工作。目前,请相信我们,您希望将金额处理为整数 - 可能将所有内容转换为美分(或者该便士是您的货币吗?)然后从那里取出。
  • 请使用完整的错误回溯更新您的问题。
  • 15 岁的前两个 if 语句都将返回 True。 15 岁的人的价格是多少?

标签: python calculator flowchart


【解决方案1】:

def print_discount_message(折扣): 如果折扣 == 0.0: print('不符合家庭折扣条件。') 别的: print(' 符合折扣条件:{}%'.format(int(discount * 100)))

输出 print(' 符合折扣条件:{}%'.format(int(discount * 100))) 你应该有: print(' 符合折扣条件:{}%'.format(int((1 - discount) * 100)))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-10-05
    • 2011-08-28
    • 2021-03-12
    • 1970-01-01
    • 2023-02-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多