基于输入的Python Discount函数

问题描述

我正在尝试创建一个Python程序,根据购买的数量(您将这些书称为“书1”,“书2”等,并保存在列表中)来计算您在书上获得的折扣。该程序应接受您的输入(例如:第1本书,第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}}

当心,未经测试,您必须找到错误,但我希望它会在可行的解决方案过程中为您设置

,

尝试一下

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: ",price * 0.10,"\n")
        tPrice += (num * price)
    elif num == 4:
        tDiscount += price * 0.20
        print(" Price: ",price * 0.20,"\n")
        tPrice += num * price
    else:
        tDiscount += price * 0.25
        print(" Price: ",price * 0.25,"\n")
        tPrice += (num * price)

print("\nBill: ",tPrice,"\nDiscount: ",tDiscount,"\nTotal Bill: ",tPrice - tDiscount)

希望对您有所帮助

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...