我正在尝试使用字典在python中制作一个货币转换器程序。当使用condict.values(UserOrigin-1)时,我收到一个错误,说'builtin_function_or_method'不是subscriptable '。这个错误意味着什么,我该如何修复它?
def Currency():
condict={
"USD": 1,
"Euro": 1.21,
"Yen": 0.0094,
"UK Pound": 1.39,
"Swiss Franc": 1.12,
}
UserOrigin=int(input("Write the number to indicate which is the original currency.n1. US. Dollarsn2. Euron3. Yenn4. UK Poundn5. Swiss Francn"))
UserResult=int(input("Write the number to indicate which is the currency to convert to.n1.US. Dollarsn2. Euron3. Yenn4. UK Poundn5. Swiss Francn"))
UserAmount=int(input("Enter the amount:n"))
USDAmount=UserAmount*condict.values[UserOrigin-1]
FinalAmount=USDAmount/condict.values[UserResult-1]
print("{} {}s are {} {}s".format(UserAmount, condict.key[UserOrigin-1],FinalAmount,condict.key[UserResult-1]))
Currency()
values()
是dict
的方法,而不是属性。在对结果进行任何操作之前,必须用圆括号调用它。
而且,values()
的结果是一个迭代器,不能切片。您必须使用list()
:
USDAmount = UserAmount * list(condict.values())[UserOrigin-1]
FinalAmount = USDAmount / list(condict.values())[UserResult-1]
同样适用于keys()
:
print("{} {}s are {} {}s".format(UserAmount, list(condict.key())[UserOrigin-1],FinalAmount,list(condict.key())[UserResult-1]))