我正在制作货币转换器。如何让 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()
我如何做到的简单示例:
number = input("Enter a number: ")
if number == int:
print("integer")
if number == float:
print("float")
这两个不行
原文由 Katrina 发布,翻译遵循 CC BY-SA 4.0 许可协议
我真的希望我没有完全误解这个问题,但我走了。
看起来您只是想确保传入的值可以像浮点数一样进行操作,无论输入是
3
还是4.79
例如,对吗?如果是这种情况,则只需在对其进行操作之前将输入转换为浮点数。这是您修改后的代码:(为了整洁起见,我也做了一些改动,希望你不要介意)