我正在制作货币转换器。 如何让python接受整数和浮点数?
这就是我做的方式:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26def 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()
我是如何做到这一点的简单例子:
1
2
3
4
5
6number = 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' ...
这是你如何检查给定的字符串并接受int或float(并且还转换为它; nb将是int或float):
1
2
3
4
5
6
7
8
9
10number = input("Enter a number:")
nb = None
for cast in (int, float):
try:
nb = cast(number)
print(cast)
break
except ValueError:
pass
但在你的情况下,只使用float可能会起作用(因为整数的字符串表示也可以转换为浮点数:float('3') -> 3.0):
1
2
3
4
5
6
7number = input("Enter a number:")
nb = None
try:
nb = float(number)
except ValueError:
pass
如果nb None,则会收到无法转换为float的内容。
为什么另一个人说:"当然,真正的Pythonic解决方案是鸭子打字并且如果非int / float被传递则会捕获错误!"?你能解释一下吗?我是编程新手。
这与鸭子打字无关。我只是尝试以永不崩溃的方式将字符串转换为浮点数。其中一个Python哲学是EAFP而不是LBYL。因此python编码器通常会try而不是先检查事物。 (例如,如果你想将str转换为int,你可以先检查字符串是否只包含数字;这不是pythonic的事情)。
我真的希望我不会完全误解这个问题但是我走了。
看起来您只是想确保传入的值可以像浮点一样操作,无论输入是3还是4.79,例如,是否正确?如果是这种情况,那么只需将输入转换为浮点数,然后再对其进行操作。这是你修改过的代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18def 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)
amount==int没有意义。 input给了我们一个字符串。 int(和float)是一个函数。字符串永远不等于函数。
1
2
3
4
5
6
7
8
9In [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 .23",则使用float(x)将其转换为数字会更安全。结果将是一个浮点数。对于许多计算,您无需担心它是浮点数还是整数。数学是一样的。
如果需要,您可以在int和float之间进行转换:
1
2
3
4
5
6
7
8In [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测试
1
2
3
4
5
6
7
8In [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
但你可能不需要做任何这些。
使用内置的isinstance函数
1
2if isinstance(num, (int, float)):
#do stuff
此外,您应该避免为变量名使用保留关键字。关键字from是Python中的保留关键字
最后,还有一个我注意到的错误:
1if From == 'aud' or 'brl'
应该
1if From == 'aud' or From == 'brl'
最后,要清理if语句,理论上可以使用该列表(如果将来有更多货币,这可能会更好。
1
2
3currencies = ['aud', 'brl'] #other currencies possible
if From in currencies and to in currencies:
#do conversion
isinstance(num, (int, float))可以直接完成......看起来OP输入以字符串开头。
@hiroprotagonist当然,真正的Pythonic解决方案是鸭子打字并且如果传递非int / float则会捕获错误!
我同意并试图按照以下方式提出答案......
如果在if语句中没有指定货币,我将如何转换它?
@KGarcia你必须重新调整你的代码,这超出了这个评论的范围,但是如果你计划转换多种货币,那么你设置你的功能的方式效率很低