Python 类型错误:';内置函数或方法';对象不支持项分配
尝试运行脚本时出现以下错误:Python 类型错误:';内置函数或方法';对象不支持项分配,python,function,python-3.x,Python,Function,Python 3.x,尝试运行脚本时出现以下错误: 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 exchan
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
我尝试将一个主功能设置为菜单,然后调用其他子功能来完成相关选择。我想就这是否是一个好的方法和代码,因为我看到我的老师做了类似的事情,我是否应该把它放在'如果'语句等一些建议
这是一种好的、正确的编码方式吗?currencyList.index是一种方法参考;要为列表编制索引,请删除
.index
部分:
crntItem = currencyList[crntCurrency]
print(crntItem)
newItem = currencyList[newCurrency]
我怀疑您试图在列表中查找crntCurrency
的索引,然后添加1以查找值:
crntItem = currencyList[currencyList.index(crntCurrency) + 1]
currencies = {"Pound Sterling": 1, "Euro": 1.22, "US Dollar": 1.67, "Japanese Yen": 169.9480}
及
但也许你应该在这里使用字典:
crntItem = currencyList[currencyList.index(crntCurrency) + 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: ")
什么是完整的回溯?
currencyList.index[…]
,使用()
调用函数。@AshwiniChaudhary:然后是=+1
,这不是有效的Python语法。@MartijnPieters我注意到,有一段时间你在回答中也包含了无效的语法p@AshwiniChaudhary:嘘,你没看到,真的-PTypeError:列表索引必须是整数,而不是str@user3165683此处crntCurrency=(“请输入当前货币:”)
您忘记了输入