2014-02-15 72 views
0

我有这样的错误,当我尝试运行我的脚本:类型错误:“builtin_function_or_method”对象不支持项目分配

Traceback (most recent call last): 
    File "<pyshell#6>", line 1, in <module> 
    converter() 
    File "C:\Users\Joan\Documents\School\Computing\Coursework\A453 - Python\Currency Converter.py", line 19, in converter 
    exchange(currencyList) 
    File "C:\Users\Joan\Documents\School\Computing\Coursework\A453 - Python\Currency Converter.py", line 33, in exchange 
    crntItem = currencyList.index[crntCurrency] =+ 1 
TypeError: 'builtin_function_or_method' object does not support item assignment 

这里是我的代码,一个未完成的货币换算:

# Global ist 
currencyList = ["Pound Sterling", 1, "Euro", 1.22, "US Dollar", 1.67, "Japanese Yen", 169.9480] 



################################# 
# A program to convert currency # 
# Main function     # 
################################# 
def converter(): 
    currencyList = ["Pound Sterling", 1, "Euro", 1.22, "US Dollar", 1.67, "Japanese Yen", 169.9480] 
    print("1) Enter an amount to be exchanged.") 
    print("2) Change exchange rates.") 
    choice=0 
    while choice==0: 
     selected=int(input("Please select an option: ")) 
     if selected == 1: 
      choice = 1 
      exchange(currencyList) 




################################# 
# Giving exchanged rate   # 
################################# 

def exchange(currencyList): 
    crntAmnt = int(input("Please enter the amount of money to convert: ")) 
    crntCurrency = ("Please enter the current currency: ") 
    newCurrency = ("Please enter the currency you would like to convert to: ") 
    listLength = len(currencyList) 
    crntItem = currencyList.index[crntCurrency] =+ 1 
    print(crntItem) 
    newItem = currencyList.index[newCurrency] =+ 1 
    print(newItem) 

我试图设置一个主要功能作为菜单,然后调用其他子功能来完成相关的选择。我希望得到一些建议,说明这是否是一种好的方法,因为我看到我的老师做了类似的事情,我应该把它写进'if'陈述等。

这是一个好的,正确的方法编码?

+0

什么是* full * traceback? –

+0

'currencyList.index [..]',使用'()'调用一个函数。 –

+0

@AshwiniChaudhary:然后就是'= + 1',这是不正确的Python语法.. –

回答

2

currencyList.index是方法的参考;索引列表,删除.index部分:

crntItem = currencyList[crntCurrency] 
print(crntItem) 
newItem = currencyList[newCurrency] 

嫌疑你正在努力寻找列表中的crntCurrency指标,再加入1找到替代的价值:

crntItem = currencyList[currencyList.index(crntCurrency) + 1] 

newItem = currencyList[currencyList.index(newCurrency) + 1] 

但也许你应该在这里使用字典来代替:

currencies = {"Pound Sterling": 1, "Euro": 1.22, "US Dollar": 1.67, "Japanese Yen": 169.9480} 

此货币名称映射到一个号,所以现在你可以只是仰望货币,而无需摆弄指数:

crntItem = currencies[crntCurrency] 

哦,你忘了采取实际用户输入;当请求货币转换时添加input()呼叫:

crntCurrency = input("Please enter the current currency: ") 
newCurrency = input("Please enter the currency you would like to convert to: ") 
+0

然后我得到的错误 – user3165683

+0

TypeError:列表索引必须是整数,而不是str – user3165683

+0

@ user3165683这里'crntCurrency =(“请输入目前的货币:“)'你忘记了'输入' – zhangxaochen

相关问题