功能:Python中的计算价格 - 抽屉价格

时间:2016-04-21 01:19:10

标签: python function

我需要做一个有功能的程序:

  • 接受桌面上的抽屉数量作为键盘输入。此函数返回main()函数的抽屉数。

  • 接受输入并返回木材类型 - 桃花心木为'm',橡木为'o',松木为'p'。

  • 将抽屉数量和木材类型作为参数,并计算桌子的成本

  • 显示所有详细信息和最终价格。

  • 主要功能

我已经只有主要功能的成本。我只是不知道如何将它分成四个不同的功能。 这是我的代码:

def main(): 

    r = int(input("Enter number of drawers >> ")) #prompting user for input
    extra = 30 * r
    drawers = input("Enter type of wood - m, o, or p >> ") 
    if drawers == 'm':
        ans = 180 + extra
        if r == 1: #if statement
            print("You have ordered a mahogany desk with 1 drawer")
        else:
            print("You have ordered a mahogany desk with",r,"drawers")


    if drawers == 'p':#if statement
        ans = 100 + extra
        if r == 1:#if statement
            print("You have ordered a pine desk with 1 drawer")
        else:
            print("You have ordered a pine desk with",r,"drawers")

    if drawers == 'o':#if statement
        ans = 140 + extra
        if r == 1:#if statement
            print("You have ordered a oak desk with 1 drawer")
        else:
            print("You have ordered a oak desk with",r,"drawers")

    elif drawers == 'o':
        ans = 140 + extra
    elif drawers == 'p':
        ans = 100 + extra
    print("Total price is $"+str(ans)) #printing total price

main()

1 个答案:

答案 0 :(得分:0)

也许你可以使用这个简单的草图并填写你现有的逻辑:

def drawer_count():
    drawers = int(input("How many drawers?")
    return drawers

def wood_type():
    wood = input("Which type of wood")
    return wood

def calc_price(wood, drawers):
    ...
    return total

def checkout(wood, drawers, total):
    print(...)

def main():
    wood = wood_type()
    drawers = drawer_count()
    total = calc_price(wood, drawers)
    checkout(wood, drawers, total)