【问题标题】:Python Discount function based on input基于输入的Python折扣函数
【发布时间】:2020-08-31 07:46:38
【问题描述】:

我正在尝试创建一个 Python 程序,该程序根据您购买的图书数量计算您收到的图书折扣(为此,图书被称为“图书 1”、“图书 2”等,并保存在列表中) .该程序应接受您的输入(例如:第 1 册、第 2 册) 如果同一本书购买了两次,则没有折扣,但如果购买了 2 种或更多不同的书籍,则五本书中的任何一本的单本价格为 8 欧元

  • 如果购买该系列的两本不同书籍,则可享受 5% 的折扣
  • 如果购买该系列的三本不同书籍,则可享受 10% 的折扣
  • 如果购买该系列的四本不同书籍,则可享受 20% 的折扣
  • 如果购买该系列的所有五本书,则可享受 25% 的折扣
  • 如果客户购买一本书的两份,第二份的费用为 8 欧元,并且不会应用任何折扣,除非另一本书的副本更多,可以作为第二套书籍的一部分应用折扣。

我的代码在下面,不知道哪里出了问题,因为我遇到了很多语法错误,而且我认为我没有所需的所有信息。

#list of books
books = ["book 1", "book 2", "book 3", "book 4", "book 5"]
if books <= 1
    print("One book is 8 EURO - no discount")
elif books <= 2
    books discount =books*0.05
elif books <= 3
    books discount =books*0.10
elif books <= 4
    books discount =books*0.20
elif books <= 5
    books discount =books*0.25

print("Discount : ",discount)
print("Total cost  : ",price -discount)

else:
print("no discount if two books of the same title are purchased")

【问题讨论】:

  • 1.您的列表仅包含字符串值,请考虑使用字典,以便您可以分隔值,或者仅使用仅包含值的列表,例如 [1, 2, 3],或者如果您想要书籍的数量,请使用 len( )。 2.您的打印语句指的是您从未设置过的变量。 3.你的vars必须是一个没有空格的单词,所以“books discount”不起作用。首先尝试这三件事,你应该在那里,或者非常接近
  • 我强烈建议你先看一个基本的python教程,我认为你的许多语法问题都会得到解决。一些不错的链接给你linklinklink
  • 首先,请阅读How to Ask不确定我哪里出错了 并不完全是一个问题陈述。如果您有任何错误 - 发布它们。如果您的输出是错误的 - 显示它以及您期望它是什么。其次,您似乎应该阅读一些基本教程,并且可能想从the official one 开始。对于初学者,books discount 应该是 books_discount。名称中不能有空格

标签: python python-3.x discount


【解决方案1】:

我会这样做:

def discount_rule(booklist, price):
    discount_rates = [0, 0, 0.05, 0.1, 0.2, 0.25]
    different_books = len(set(booklist))
    discount = discount_rates[different_books]
    discount_amount = discount * price;
    discounted_price = price - discount_amount;
    return {"grandTotal": len(books) * discounted_price,
            "discount": discount_amount,
            "book_price": price - discounted_price
            "books" : different_books
    }

  
#list of books
books = ["book 1", "book 2", "book 3", "book 4", "book 5"]
unit_price = 8
cash = discount_rule(books, unit_price)

if cash['books'] == 1:
    print ("One book is {} EURO - no discount".format(unit_price))
print("Discount : ", cash["discount"])
print("Total cost  : ", cash["grandTotal"])
if len(books) > cash["books"]:
    print("no discount if two books of the same title are purchased")

当心,没有经过测试,你必须找到错误,但我希望它能帮助你找到可行的解决方案

【讨论】:

    【解决方案2】:

    试试看

    books =["book 1", "book 2", "book 3", "book 4", "book 5"]
    price = 8
    
    nos = len(books)
    purchase = []
    tDiscount = 0
    tPrice = 0
    
    for i in range(1, nos+1):
        print("Enter quantity of book ", i, ": ")
        num = int(input())
        if num == 0:
            tDiscount += 0
            print(" Price: 0\n Discount: 0\n")
        elif num == 1:
            print(" One book is 8 EURO - no discount\n Price: ", price, "\n")
            tPrice += price
        elif num == 2:
            tDiscount += price * 0.05
            print(" Price: ", num * price, "\n Discount : ", price * 0.05, "\n")
            tPrice += (num * price)
        elif num == 3:
            tDiscount += price * 0.10
            print(" Price: ", num * price, "\n Discount : ", price * 0.10, "\n")
            tPrice += (num * price)
        elif num == 4:
            tDiscount += price * 0.20
            print(" Price: ", num * price, "\n Discount : ", price * 0.20, "\n")
            tPrice += num * price
        else:
            tDiscount += price * 0.25
            print(" Price: ", num * price, "\n Discount : ", price * 0.25, "\n")
            tPrice += (num * price)
    
    print("\nBill: ", tPrice, "\nDiscount: ", tDiscount, "\nTotal Bill: ", tPrice - tDiscount)
    

    希望对您有所帮助

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-23
      • 1970-01-01
      • 1970-01-01
      • 2017-04-13
      • 2013-04-16
      • 2020-08-30
      • 2021-05-18
      相关资源
      最近更新 更多