2017-04-22 28 views
2

我正在做一個貨幣轉換器。我如何讓Python接受整數和浮點數?如何接受int和float類型的輸入?

這是我做的:

def aud_brl(amount,From,to): 
    ER = 0.42108 
    if amount == int: 
     if From.strip() == 'aud' and to.strip() == 'brl': 
      ab = int(amount)/ER 
     print(ab) 
     elif From.strip() == 'brl' and to.strip() == 'aud': 
      ba = int(amount)*ER 
     print(ba) 
    if amount == float: 
     if From.strip() == 'aud' and to.strip() == 'brl': 
      ab = float(amount)/ER 
     print(ab) 
     elif From.strip() == 'brl' and to.strip() == 'aud': 
      ba = float(amount)*ER 
     print(ba) 

def question(): 
    amount = input("Amount: ") 
    From = input("From: ") 
    to = input("To: ") 

    if From == 'aud' or 'brl' and to == 'aud' or 'brl': 
     aud_brl(amount,From,to) 

question() 

的我是如何做的簡單的例子:

number = input("Enter a number: ") 

if number == int: 
    print("integer") 
if number == float: 
    print("float") 

這兩個不工作。

+1

我改變你的標題和標題爲小寫。請不要向我們大喊:) – gyre

+0

'如果type(number)是int'但是這總是錯誤的,因爲'number'將始終是一個字符串。 –

+0

@ juanpa.arrivillaga不,它不是。他使用'input'從用戶讀取,'type(numer)'是'str'。 – direprobs

回答

2

我真的很希望我不會完全誤解這個問題,但我在這裏。

看起來你只是想確保傳入的值可以像float一樣操作,不管輸入是3還是4.79例如,是否正確?如果是這種情況,那麼只需在輸入之前將其輸入爲浮點型。下面是修改後的代碼:

def aud_brl(amount, From, to): 
    ER = 0.42108 
    if From.strip() == 'aud' and to.strip() == 'brl': 
     result = amount/ER 
    elif From.strip() == 'brl' and to.strip() == 'aud': 
     result = amount*ER 

    print(result) 

def question(): 
    amount = float(input("Amount: ")) 
    From = input("From: ") 
    to = input("To: ") 

    if (From == 'aud' or From == 'brl') and (to == 'aud' or to == 'brl'): 
     aud_brl(amount, From, to) 

question() 

(我做了一些變化,以及對整潔的緣故,我希望你不介意< 3)

0

使用isinstance功能,這是建立在

if isinstance(num, (int, float)): 
    #do stuff 

此外,你應該使用保留的關鍵字作爲變量名不要。關鍵字from是Python中保留關鍵字

最後,還有一個其他錯誤,我注意到:

if From == 'aud' or 'brl' 

應該

if From == 'aud' or From == 'brl' 

最後,清理if語句理論上你就可以使用清單(如果您將來有更多的貨幣,這可能會更好。)

currencies = ['aud', 'brl']  #other currencies possible 
if From in currencies and to in currencies: 
    #do conversion 
+0

'isinstance(num,(int,float))'可以直接完成...並且看起來好像OP輸入以字符串開始。 –

+0

@hiroprotagonist當然,真正的Pythonic解決方案當然是鴨子打字,如果非int/float通過,會出現錯誤! –

+0

我同意,並試圖想出沿着這些線路的答案... –

2

這是你如何檢查定的字符串,接受intfloat(也投它; nb將是一個intfloat):

number = input("Enter a number: ") 

nb = None 
for cast in (int, float): 
    try: 
     nb = cast(number) 
     print(cast) 
     break 
    except ValueError: 
     pass 

但在你的情況下,只需使用浮動可能做的伎倆(如也整數的字符串表示可以轉換爲浮動:float('3') -> 3.0):

number = input("Enter a number: ") 

nb = None 
try: 
    nb = float(number) 
except ValueError: 
    pass 

如果nbNone,你得到的東西不能被轉換爲float

+0

誰叫其他人說:「當然,真正的Pythonic解決方案將會是鴨子打字,並且如果非int/float被傳遞,會出現錯誤!」?你能解釋一下嗎?我是編程新手。 – Katrina

+0

這與鴨子打字無關。我只是嘗試將一個字符串轉換爲一個永遠不會崩潰的方式。蟒理念之一是[EAFP](https://docs.python.org/3/glossary.html#term-eafp),而不是[LBYL](https://docs.python.org/3/glossary的.html#長期-lbyl)。所以python編碼者經常會「嘗試」某種東西,而不是首先檢查事物。 (例如,如果你想將'str'轉換爲'int',你可以首先檢查字符串是否只包含數字;這不是一個pythonic的事情)。 –

0

amount==int沒有意義。 input給了我們一個字符串。 int(和float)是一個函數。一個字符串從不等於一個函數。

In [42]: x=input('test') 
test12.23 
In [43]: x 
Out[43]: '12.23' 
In [44]: int(x) 
.... 
ValueError: invalid literal for int() with base 10: '12.23' 
In [45]: float(x) 
Out[45]: 12.23 

float('12.23')返回float對象。int('12.23')產生一個錯誤,因爲它不是一個有效的整數字符串格式。

如果用戶可能會給要麼「12」或'12 0.23' ,是比較安全的使用float(x)將其轉換爲數字。結果將是一個浮動。對於許多計算,你不必擔心它是浮點還是整數。數學是一樣的。

您可以根據需要INT之間的轉換和浮動:

In [45]: float(x) 
Out[45]: 12.23 
In [46]: float(12) 
Out[46]: 12.0 
In [47]: int(12.23) 
Out[47]: 12 
In [48]: round(12.23) 
Out[48]: 12 

你也可以做instance測試

In [51]: isinstance(12,float) 
Out[51]: False 
In [52]: isinstance(12.23,float) 
Out[52]: True 
In [53]: isinstance(12.23,int) 
Out[53]: False 
In [54]: isinstance(12,int) 
Out[54]: True 

但你可能並不需要做任何的那些。

相關問題