货币转换器

时间:2017-02-09 20:44:27

标签: python

我遇到一个问题,即程序无法显示用户想要转换的所需金额。另外,你如何将数字四舍五入到小数点后三位呢?

money = int(input("Enter the amount of money IN GDP you wish to convert :"))

USD = 1.25
EUR = 1.17
RUP = 83.87
PES = 25.68

currency = input("Which currency would you like to convert the money into?")

if currency == "USD":
    print(money) * USD
elif currency == "EUR":
    print(money) * EUR
elif currency == "RUP":
    print(money) * RUP
elif currency == "PES":
    print(money) * PES

1 个答案:

答案 0 :(得分:0)

Python包含round()函数lets you specify您想要的位数。因此,您可以使用round(x, 3)进行正常舍入,最多可达到3位小数。

示例

print(round(5.368757575, 3)) # prints 5.369

<强>更新

您可以通过这种方式更新代码。

money = int(input("Enter the amount of money IN GDP you wish to convert: "))

USD = 1.25
EUR = 1.17
RUP = 83.87
PES = 25.68

currency = input("Which currency you like to convert the money into?: ")

if currency == "USD":
    print(round(money * USD, 3))
elif currency == "EUR":
    print(round(money * EUR, 3))
elif currency == "RUP":
    print(round(money * RUP, 3))
elif currency == "PES":
    print(round(money * PES, 3))

输出:

Enter the amount of money IN GDP you wish to convert: 100
Which currency you like to convert the money into?: USD
125.0

Enter the amount of money IN GDP you wish to convert: 70
Which currency you like to convert the money into?: RUP
5870.9