2016-04-21 47 views
0

的,我需要做的是有功能的程序:功能:計算價格在Python-價格抽屜

  • 接受抽屜的辦公桌從鍵盤輸入的號碼。該函數返回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() 

回答

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)