我正在尝试创建一个Python脚本,用于计算在给出书籍列表时要支付的最终金额。 [英] I am trying to create a Python script which calculates the final amount to be paid when given a list of books.

查看:79
本文介绍了我正在尝试创建一个Python脚本,用于计算在给出书籍列表时要支付的最终金额。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个菜单,用户可以从列表中选择多个选项。并申请最后的折扣。使用此参数:

2000年以后出版的所有书籍都有10%的折扣。 •购买总价值超过30英镑的书籍,可获得5%的折扣。



可以使用以下书籍•xxxx(1851)£15.20•xxxx(2010)£13.14•xxxx(1980)£11.05•xxxxx(1976)£10.24• xxxxx(1889)£12.87•xxxxx(1895)£10.43•xxxx(1954)£8.12•xxxx(1886)£7.32•xxxxx(1843)£4.23•xxxx(1859)£6.32•xxx(1861)£13.21



这是我第一次尝试编码,请放轻松。在此先感谢。



我尝试过:



I want to create a menu where the user can select multiple options from the list. and apply a final discount. with this parameters:
All books published after 2000 have 10% discount. • Buy books worth more than £30 in total, get a 5% discount on the total.

The following books are available • xxxx (1851) £15.20 • xxxx (2010) £13.14 • xxxx (1980) £11.05 • xxxxx (1976) £10.24 • xxxxx (1889) £12.87 • xxxxx (1895) £10.43 • xxxx (1954) £8.12 • xxxx (1886) £7.32 • xxxxx (1843) £4.23 • xxxx (1859) £6.32 • xxx (1861) £13.21

This is my first try at coding, please go easy on me. Thanks in advance.

What I have tried:

menu = 3
while menu == 3:
    print ("Select book option")
    print ("1 Moby Dick (1851)- £15.20")
    print ("2 The Terrible Privacy of Maxwell Sim (2010) - £13.14")
    print ("3 Still Life With Woodpecker (1980) - £11.05")
    print ("4 Sleeping Murder (1976) - £10.24")
    print ("5 Three Men in a Boat (1889) - £12.87")
    print ("6 The Time Machine (1895) - £10.43")
    print ("7 The Caves of Steel (1954) - £8.12")
    print ("8 Idle Thoughts of an Idle Fellow (1886) - £7.32")
    print ("9 A Christmas Carol (1843) - £4.23")
    print ("10 A Tale of Two Cities (1859) - £6.32")
    print ("11 Great Expectations (1861) - £13.21")
    
def option():
    while True:
        option = int(input('select an option from 1 to 11 '))
        if option > 11 or option < 1:
            print ("Option not avialable. Try again! ")
            option()
        elif option == 1:
            print ("You select option 1 Moby Dick (1851)- £15.20")
            cont = input('Enter Y to add another option or N to finish: ')
            if cont == "N":
                   exit
            elif cont == "Y":
                   menu = 3
                   
            else:
                   break
        elif option == 2:
            print ("You select option 1 Moby Dick (1851)- £15.20")
            cont = input('Enter Y to add another option or N to finish: ')
            def dis10(option, dis):
                option = 13.14
                dis = option * 0.1
                dis10 = option - dis
            if cont == "N":
               exit
            elif cont == "Y":
               menu = 3
            else:
                   break
        elif option == 3:
            print ("You select option 1 Moby Dick (1851)- £15.20")
            cont = input('Enter "Y" to add another option or "N" to finish: ')
            if cont == "N":
               exit
            elif cont == "Y":
               menu = 3
            else:
                   break
        elif option == 4:
            print ("You select option 1 Moby Dick (1851)- £15.20")
            cont = input('Enter "Y" to add another option or "N" to finish: ')
            if cont == "N":
               exit
            elif cont == "Y":
               menu = 3
            else:
                   break
        elif option == 5:
            print ("You select option 1 Moby Dick (1851)- £15.20")
            cont = input('Enter "Y" to add another option or "N" to finish: ')
            if cont == "N":
               exit
            elif cont == "Y":
               menu = 3
            else:
                   break
        elif option == 6:
            print ("You select option 1 Moby Dick (1851)- £15.20")
            cont = input('Enter "Y" to add another option or "N" to finish: ')
            if cont == "N":
               exit
            elif cont == "Y":
               menu = 3
            else:
                   break
        elif option == 7:
            print ("You select option 1 Moby Dick (1851)- £15.20")
            cont = input('Enter "Y" to add another option or "N" to finish: ')
            if cont == "N":
               exit
            elif cont == "Y":
               menu = 3
            else:
                   break
        elif option == 8:
            print ("You select option 1 Moby Dick (1851)- £15.20")
            cont = input('Enter "Y" to add another option or "N" to finish: ')
            if cont == "N":
               exit
            elif cont == "Y":
               menu = 3
            else:
                   break
        if option == 9:
            print ("You select option 1 Moby Dick (1851)- £15.20")
            cont = input('Enter "Y" to add another option or "N" to finish: ')
            if cont == "N":
               exit
            elif cont == "Y":
               menu = 3
            else:
                   break
        elif option == 10:
            print ("You select option 1 Moby Dick (1851)- £15.20")
            cont = input('Enter "Y" to add another option or "N" to finish: ')
            if cont == "N":
               exit
            elif cont == "Y":
               menu = 3
            else:
                   break
        elif option == 11:
            print ("You select option 1 Moby Dick (1851)- £15.20")
            cont = input('Enter "Y" to add another option or "N" to finish: ')
            if cont == "N":
               exit
            elif cont == "Y":
               menu = 3
            else:
                   break
def totaldis(total_amount, totaldis):
    totaldis = total_amount - 

推荐答案

您需要学习如何使用模块( 6。模块 - Python 3.4.8文档 [ ^ ])和循环(参见 4.更多控制流工具 - Python 3.4.8文档 [< a href =https://docs.python.org/3.4/tutorial/controlflow.html\"target =_ blanktitle =New Window> ^ ])。因为它有很多重复的代码,这使得你的程序更难以阅读。从包含书籍名称及其价格的列表开始 - 请参阅 5。数据结构 - Python 3.4.8文档 [ ^ ]对于某些不同的选项。然后,您可以通过列出列表中的条目动态构建菜单,并在其位置作为选择值之前。当用户输入一个数字时,您将可以显示相应项目的详细信息。



当用户选择每个项目时,您需要保持总计花费的金额(在应用折扣之前,之后或两者之间)。然后,当用户选择不再时,您可以检查是否应该应用额外的5%折扣。
You need to learn how to use modules (6. Modules — Python 3.4.8 documentation[^]) and loops (see 4. More Control Flow Tools — Python 3.4.8 documentation[^]). As it is you have a lot of duplicate code which just makes your program more difficult to read. Start with a list that contains the names of the books and their price - see 5. Data Structures — Python 3.4.8 documentation[^] for some of the different options. You can then construct your menu dynamically by listing the entries in the list, preceded by their position as the selection value. When the user enters a number, that will allow you to show the details of the appropriate item.

As the user chooses each item you need to keep a running total of the amount spent (either before the discount is applied, after, or both). Then When the user selcts "no more" you can check whether the additional 5% discount should be applied.


这篇关于我正在尝试创建一个Python脚本,用于计算在给出书籍列表时要支付的最终金额。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆