【发布时间】:2017-04-22 20:13:10
【问题描述】:
我正在做一个货币转换器。如何让 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")
这两个不起作用。
【问题讨论】:
-
我把你的标题和标题改成了小写。请不要对我们大喊大叫:)
-
if type(number) is int但这永远是错误的,因为number永远是一个字符串。 -
@juanpa.arrivillaga 不,不是。他使用
input读取用户,type(numer)是str。 -
您知道,
if From == 'aud' or 'brl' and to == 'aud' or 'brl'行将始终计算为True,因为'brl'在这两种情况下都是真实的。如果您想查看From是'aud'还是'brl',您需要这样的东西:if From == 'aud' or From == 'brl' ...
标签: python python-3.x integer