2017-09-02 86 views
-2

當我在Python編程新手,有這個簡單的程序,計算瓷磚價格給予2個維度麻煩:NameError:名字「成本」是沒有定義 - 調用函數

Objective: Calculate the total cost of tile it would take to cover a floor plan of width and height, using a cost entered by the user.

print ("NOTE: The unit of cost is in dollars and dimension unit is in feet") 

def cost_o_tile(cost, width, height): 
    while True: 
     cost = int(input("Cost of each tile:")) 
     width = int(input("What is the width of the floor?")) 
     height = int(input("What is the height of the floor?")) 
     try: 
      if cost < 0 or width < 0 or height <0: 
       print ("\n Please enter non-negative integers") 
       break 
      else: 
       return ("In order to cover your {} X {} floor, you will need to pay {} dollars".format(width,height,cost*width*height)) 
     except ValueError: 
      print ("No valid integer! Please try again ...") 


cost_o_tile(cost, width, height) 

我理解我可以在函數之外聲明變量,並且代碼可以工作。但是,我想在循環內部使用這些變量,因此可以通過except ValueError進行驗證。

+0

你在哪裏定義成本?如果這就是你想要計算的成本,爲什麼你要花費呢?當你寫'cost_o_tile(cost,width,height)'時,你在給函數數據。 – Carcigenicate

+0

@Carcigenicate:他每個瓷磚的成本(作爲參數傳遞)和整層的成本(他正在計算的)。 –

+0

@HughBothwell Ah – Carcigenicate

回答

1

那麼,你的功能cost_o_lite不應該採取任何參數:

def cost_o_tile(): 
    ... 

print(cost_o_tile()) 

而且不要忘了打印結果。

你也可以單獨關注:

先寫一個算法,計算總成本:

def cost_o_tile(cost, width, height): 
    if cost < 0 or width < 0 or height < 0: 
     raise ValueError 
    return cost * width * height 

然後編寫用戶界面代碼:

print ("NOTE: The unit of cost is in dollars and dimension unit is in feet") 

while True: 
    try: 
     cost = int(input("Cost of each tile:")) 
     width = int(input("What is the width of the floor?")) 
     height = int(input("What is the height of the floor?")) 
     total = cost_o_tile(cost, width, height) 
     print("In order to cover your {} X {} floor, you will need to pay {} dollars" 
       .format(width, height, total)) 
     break 
    except ValueError: 
     print ("No valid integer! Please try again ...") 
+0

這個工程!謝謝。在定義它們之前,我不應該在參數中傳遞參數。 –

+1

@ThienNghiem儘管這不是很好的編程習慣。 –

0
cost_o_tile(cost, width, height) 

的問題是,當你調用你的代碼行18 cost_o_tile功能cost說法。如果仔細觀察,它並未在函數範圍之外定義,因此是錯誤。

1

讓您的功能一樣純淨可能是編寫良好可維護代碼的關鍵,而且這當然不包括潛在的無限循環和用戶輸入。

在全球範圍內,沒有變量cost,widthheight應該存在。這是你錯誤的原因。

  1. 輸入的代碼必須外移
  2. 消除環路
  3. 您可以不用擔心錯誤處理,一旦你得到你的代碼工作。

首次通過

def cost_o_tile(cost, width, height): 
    return ("In order to cover your {} X {} floor, you will need to pay {} dollars"\ 
          .format(width, height, cost * width * height)) 

cost, width, height = map(int, input("Enter 3 space separated integers: ").split()) 
print(cost_o_tile(cost, width, height)) 

第二道

一旦你有一個基本的程序工作,你可以看看錯誤處理:

def cost_o_tile(cost, width, height): 
    try: 
     if cost < 0 or width < 0 or height < 0: 
      return "Parameters cannot be lesser than 0" 
    except ValueError: 
     return "Please provide numbers only" 

    return ("In order to cover your {} X {} floor, you will need to pay {} dollars"\ 
          .format(width, height, cost * width * height)) 

cost, width, height = map(int, input("Enter 3 space separated integers: ").split()) 
print(cost_o_tile(cost, width, height)) 

最後一傳

現在,隨着搬運就位錯誤,你終於可以看一下環。

def cost_o_tile(cost, width, height): 
    try: 
     if cost < 0 or width < 0 or height < 0: 
      return "Parameters cannot be lesser than 0" 
    except ValueError: 
     return "Please provide numbers only" 

    return ("In order to cover your {} X {} floor, you will need to pay {} dollars"\ 
          .format(width, height, cost * width * height)) 

if __name__ == '__main__': 
    while True: 
     cost, width, height = map(int, input("Enter 3 space separated integers: ").split()) 
     print(cost_o_tile(cost, width, height)) 

     if input("Continue? ").lower() not in {'y', 'ye', 'yes'}: 
      break 
+1

「正確答案」。當我在計算功能中看到「輸入」時,我總是畏縮不前。我認爲試圖解釋純粹和副作用雖然可能會減損實際問題。 – Carcigenicate

+0

謝謝你的建議!我會盡力遵循這個良好的編程習慣。 –

+1

IMO,將異常轉換爲結果通常不是一個好習慣(這裏的字符串值)。我更喜歡讓異常進入調用者代碼(我會說的主要功能)。看到我的答案。 –

相關問題