How to accept the input of both int and float types?

后端 未结 4 1595
广开言路
广开言路 2021-01-21 09:18

I am making a currency converter. How do I get python to accept both integer and float?

This is how I did it:

def aud_brl(amount,From,to):
    ER = 0.421         


        
4条回答
  •  我在风中等你
    2021-01-21 10:16

    Use the isinstance function, which is built in

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

    Also, you should refrain from using reserved keywords for variable names. The keyword from is a reserved keyword in Python

    Finally, there is one other error I noticed:

    if From == 'aud' or 'brl'
    

    Should be

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

    Lastly, to clean up the if statements you could theoretically use the list (if you have more currencies in the future, this might be better.

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

提交回复
热议问题