当我尝试在我的商店购物时,shell崩溃了一个" TypeError:元组索引必须是整数或切片,而不是str"信息。此外,代码运行良好,直到我尝试购买东西。
def store():
os.system('cls')
print("Welcome to the shop!")
print("What would you like to buy?")
print("1.) Greatsword - 40 gold")
print("0.) Back")
print(' ')
option = input(' ')
if option in weapons:
if PlayerIG.gold >= weapons[option]:
os.system("cls")
PlayerIG.gold -= weapons[option]
PlayerIG.weap.append(option)
print("You have bought %s!" % option)
option = input(' ')
store()
else:
os.system('cls')
print("You cannot afford this.")
option = input(' ')
store()
elif option == "Back":
start1()
else:
os.system("cls")
print("That item does not exist.")
option = input(' ')
store()
然后我收到此错误:
Traceback (most recent call last):
File "C:\Users\Chris\Desktop\Amalgia Arena\AmalgiaArena.py", line 284, in <module>
main()
File "C:\Users\Chris\Desktop\Amalgia Arena\AmalgiaArena.py", line 60, in main
start()
File "C:\Users\Chris\Desktop\Amalgia Arena\AmalgiaArena.py", line 90, in start
start1()
File "C:\Users\Chris\Desktop\Amalgia Arena\AmalgiaArena.py", line 109, in start1
store()
File "C:\Users\Chris\Desktop\Amalgia Arena\AmalgiaArena.py", line 262, in store
if PlayerIG.gold >= weapons[option]:
TypeError: tuple indices must be integers or slices, not str
我已经查看过具有相同错误及其解决方案的其他代码,但我对Python非常陌生,因此我无法弄清楚我需要修复的内容。如果还有我需要发布的代码,那么我会。另外,我是这个网站的新手,所以如果你想因为某种原因(糟糕的问题,糟糕的代码等)打击我,那么请这样做。我愿意学习:D
答案 0 :(得分:0)
weapons[int(option)]
的返回值是一个字符串,只需添加weapons
即可更改为整数。
编辑:撤消上述内容。你所做的只是将weapons = {'Greatsword', 40}
定义为元组而不是字典。只需将定义更改为module.hot.accept