【问题标题】:Applying a discount and showing discounted rate in Python 2.7在 Python 2.7 中应用折扣并显示折扣率
【发布时间】:2015-03-05 05:18:44
【问题描述】:

我有一个简短的问题要问大家。我目前正在开发一个示例航空公司预订系统,如果用户是常旅客(10% 的折扣率),我很难显示折扣总额。以下是我的代码:

user_people = int(raw_input("Welcome to Ramirez Airlines!  How many people will be flying?"))
user_seating = str(raw_input("Perfect!  Now what type of seating would your party prefer?"))
user_luggage = int(raw_input("Thanks.  Now for your luggage, how many bags would you like to check in?"))
user_frequent = str(raw_input("Got it.  Is anyone in your party a frequent flyer with us?"))
user_continue = str(raw_input("Your reservation was submitted successfully.  Would you like to do another?"))
luggage_total = user_luggage * 50


import time
print time.strftime("Date and time confirmation: %Y-%m-%d %H:%M:%S")

seats_total = 0

if user_seating == 'economy':
    seats_total = user_people * 916
    print ('The total amount for your seats is: $'),seats_total

elif user_seating == 'business':
    seats_total = user_people * 2650
    print ('The total amount for your seats is: $'),seats_total

else: 
    print ('The total amount for your seats is: $'),user_people * 5180

print ('The total amount of your luggage is: $'),luggage_total

print ('Your subtotal for your seats and luggage is $'), luggage_total + seats_total

discount_amount = 0
discount_rate = 0.10

if user_frequent == 'yes':
    before_discount = luggage_total + seats_total
    after_discount = before_discount * discount_rate
    discount_amount = before_discount - after_discount
    print discount_amount

else:
    print ('Sorry, the discount only applies to frequent flyers!')

虽然我没有收到错误,但我的输出不正确。这是正在显示的内容:

Discount amount of 1738.8

这显然是不正确的,因为这是折扣后的价格。我正在尝试显示总折扣以及应用折扣后的价格。

任何帮助将不胜感激!谢谢!

【问题讨论】:

    标签: python rate discount


    【解决方案1】:

    你有不止一个错误。首先,在第一个 ifelse 中,您计算 seat_total,因此以下计算将崩溃 - 您只需这样做

    print ('The total amount for your seats is: $'),user_people * 5180
    

    而不是明显需要的

    seat_total = user_people * 5180
    print ('The total amount for your seats is: $'), seat_total
    

    (括号是无用的,但它们没有伤害,所以我让它们成为:-)。

    其次,看看你的折扣逻辑:

    discount_rate = 0.10
    
    if user_frequent == 'yes':
        before_discount = luggage_total + seats_total
        after_discount = before_discount * discount_rate
        discount_amount = before_discount - after_discount
    

    您是在非常明确表示,用户支付了标价的 1/10 的折扣 - 然后您在 Q 中抱怨它!-)

    再一次,很明显(对于阅读字里行间的人来说——当然永远不会对计算机来说:-),这与你,你实际上的意思形成鲜明对比 是:

    discount_rate = 0.10
    
    if user_frequent == 'yes':
        before_discount = luggage_total + seats_total
        discount_amount = before_discount * discount_rate
        after_discount = before_discount - discount_amount
    

    【讨论】:

      猜你喜欢
      • 2018-02-26
      • 2017-04-13
      • 1970-01-01
      • 2018-03-08
      • 1970-01-01
      • 2015-12-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多