1

I am trying to make a currency converter program in python with the use of dictionaries. When using condict.values(UserOrigin-1), I am receiving an error that says "'builtin_function_or_method' is not subscriptable". What does this error mean, and how can I fix it?

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. Dollars\n2. Euro\n3. Yen\n4. UK Pound\n5. Swiss Franc\n"))

  UserResult=int(input("Write the number to indicate which is the currency to convert to.\n1.US. Dollars\n2. Euro\n3. Yen\n4. UK Pound\n5. Swiss Franc\n"))

  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()
ILikeShibe
  • 11
  • 1

1 Answers1

1

values() is a method of dict and not an attribute. You must call it with the parenthesis before any operation on the result.

Moreover, the result of values() is an iterator, which you cannot slice. You must transform it to a list with list() :

USDAmount = UserAmount * list(condict.values())[UserOrigin-1]
FinalAmount = USDAmount / list(condict.values())[UserResult-1]

The same goes for keys():

print("{} {}s are {} {}s".format(UserAmount, list(condict.key())[UserOrigin-1],FinalAmount,list(condict.key())[UserResult-1]))
  • Thank you! This fixed the "'builtin_function_or_method' is not subscriptable" error, however now I have a new error that says "'dict_values' object is not subscriptable". What could this new error be referring to? EDIT: The new information fixed everything. Thank you for your help! – ILikeShibe Mar 05 '21 at 17:21
  • I edited my answer, see the part about transforming the `values()` result to a list. – Quentin Coumes Mar 05 '21 at 17:23
  • This fixed everything, thank you for your help! I said this in my edit in the comment, but I didn't realize I could respond to your comment. – ILikeShibe Mar 05 '21 at 17:28