Python 货币兑换器
我遇到一个问题,程序无法显示用户想要转换的所需金额。还有,你怎么把数字四舍五入到小数点后3位,有什么想法吗Python 货币兑换器,python,Python,我遇到一个问题,程序无法显示用户想要转换的所需金额。还有,你怎么把数字四舍五入到小数点后3位,有什么想法吗 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 in
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
Python包括
round()
函数,该函数可以指定所需的位数。因此,您可以使用四舍五入(x,3)
进行正常的四舍五入,直到小数点后3位
示例
print(round(5.368757575, 3)) # prints 5.369
更新
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
这应该有助于四舍五入,不要发布代码图片,在问题本身中以格式化文本的形式发布代码。我没有投你反对票,因为我认为这是第一个问题。虽然,我应该有,但是我没有意识到你已经在这个网站上呆了几个月了,你仍然没有发布主题问题。但是,与其抱怨被否决,也许你应该努力按照指南发帖。请在原始问题中而不是在评论中发帖错误回溯。不要使用浮点数(或双倍)来表示金钱。使用十进制或整数美分。你知道我可以在我的代码中实现这一点吗?