2012-07-14 66 views
1

使用這個類:使用Python,我將如何採取一個整數參數?

class Person: 
    def __init__ (self, Name, Address, Phone, Height, Weight): 
    self.name = Name 
    self. Address = Address 
    self.Phone = Phone 
    self.Height = Height 
    self.Weight = Weight 
    self.PoundserPerInch = Height/Weight 

我怎麼會拿在參數「身高」和「體重」爲整數,這樣我可以對他們進行一些數學函數?

+0

爲什麼在將它們傳遞給函數之前它們不是整數? – geoffspear 2012-07-14 20:41:31

回答

0
class Person: 
    def __init__ (self, Name, Address, Phone, Height, Weight): 
    self.name = Name 
    self. Address = Address 
    self.Phone = Phone 
    self.Height = int(Height) # note 
    self.Weight = int(Weight) # note 
    self.PoundserPerInch = Height/Weight 

此外:

>>> int(3) 
3 
>>> int(3.14) 
3 
>>> int("3") 
3 
+0

這將參數轉換爲整數,與接受整數參數不同。 – BrenBarn 2012-07-14 20:11:55

+0

這很可能是他想要的。 – agibalov 2012-07-14 20:13:05

+0

這不是他問的。 – BrenBarn 2012-07-14 20:13:30

4

不指定在Python類型的參數。只要接受這些論點並按照你想要的方式使用它們。也就是說,只要做Height = Height + 7或任何你喜歡的。如果某人傳遞的參數不允許您對其執行的操作類型,那麼當您嘗試執行該操作時,將在運行時引發異常。

2

Python是一種動態語言..所以你可以傳遞任何參數作爲函數。

相關問題