如何同时接受int和float类型的输入?

Kat*_*ina 5 python integer python-3.x

我正在制作一个货币转换器。如何让 python 同时接受整数和浮点数?

我就是这样做的:

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()
Run Code Online (Sandbox Code Playgroud)

我如何做到这一点的简单示例:

number = input("Enter a number: ")

if number == int:
    print("integer")
if number == float:
    print("float")
Run Code Online (Sandbox Code Playgroud)

这两个不行啊

Neo*_*ard 5

我真的希望我没有完全误解这个问题,但我就在这里。

看起来您只是想确保传入的值可以像浮点数一样进行操作,无论输入是否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()
Run Code Online (Sandbox Code Playgroud)

(为了整洁,我也做了一些更改,希望您不要介意<3)