【问题标题】:Function:Compute Price in Python- Price of Drawers功能:Python计算价格-抽屉价格
【发布时间】:2016-04-21 09:32:16
【问题描述】:

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

  • 接受桌面中抽屉的数量作为键盘的输入。该函数将抽屉的数量返回给 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()

【问题讨论】:

    标签: python function


    【解决方案1】:

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

    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)
    

    【讨论】:

      猜你喜欢
      • 2018-11-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-29
      • 1970-01-01
      • 2018-01-09
      相关资源
      最近更新 更多