我试图在python中将字符串转换为整数,但它赢了';别让我

我试图在python中将字符串转换为整数,但它赢了';别让我,python,string,Python,String,itemMatrix[item1]看起来像['Name',1,3,1] itemMatrix[item1][5]看起来像3 不管我怎么做,它总是说“str不支持该项分配/操作”。有什么办法可以解决这个问题吗 谢谢你抽出时间 这不是一个完美的解决方案,但肯定会给你很大帮助 使用字典管理游戏数据,这将使事情变得更简单: #itemsInExistence defined here: itemName = input("What do you want the new item to be calle

itemMatrix[item1]看起来像['Name',1,3,1]

itemMatrix[item1][5]看起来像3

不管我怎么做,它总是说“str不支持该项分配/操作”。有什么办法可以解决这个问题吗


谢谢你抽出时间

这不是一个完美的解决方案,但肯定会给你很大帮助

使用字典管理游戏数据,这将使事情变得更简单:

#itemsInExistence defined here:
itemName = input("What do you want the new item to be called? ")
itemStats = int(input("What is its stat? "))
itemAmount = int(input("How many of it are there? "))
itemRank = int(input("What is its base rank? "))
itemStats = int(itemStats)
itemAmount = int(itemAmount)
itemRank = int(itemRank)
for i in range(itemAmount):
  itemsInExistence.append([itemName, itemStats, itemAmount, itemRank])

#an Item is randomly chosen from itemsInExistence here:
gains = random.randint(1, 5)
if gains == 2:
  gained_weapon = random.choice(itemsInExistence)
  print("You gained the item", gained_weapon)
  itemMatrix.append(gained_weapon)
  for i, item in enumerate(itemsInExistence):
    if gained_weapon == itemsInExistence[i]:
      del itemsInExistence[i]
      break

#Here I am attempting to add what was previously known as itemStats of the 2 items together:
print("Choose two items to craft with by selecting the number to the left of it. Remember 
their 3rd numbers have to match!")
item1 = input("Item 1: ")
item2 = input("Item 2: ")
item1 = int(item1)
item2 = int(item2)
--itemMatrix[item1][-5] = int(itemMatrix[item1][-5])--
#The error occurs on the line above
itemMatrix[item1][-5] += int(itemMatrix[item2][-5])
通过这种方式,您可以直接使用属性名称,而不是使用数字访问项目的属性:

items_in_existence = []

item = {}
item['name'] = input("What do you want the new item to be called? ")
item['stats'] = int(input("What is its stat? "))
item['rank'] = int(input("What is its base rank? "))
item['amount'] = int(input("How many of it are there? "))

for i in range(item['amount'])):
    items_in_existence.append(item)

请复制完整的错误消息并突出显示相关的代码行。我将尝试。我还不知道如何突出显示。哦,nvm,我没见过这种方式。为什么是-5呢?这很奇怪。我编辑它是为了显示为什么它是-5。为什么你要使用-5作为索引?在我完成这个实现之前需要一段时间,但我完成后会给你回复。好的,如果你有任何问题,请告诉我。我打赌这会让一切变得容易。对不起,我才刚刚开始。照明+补偿。这是一门很长的课。有没有什么方法可以让字典在分配到不同的列表时继续使用?当itemsInExistence中的项被转移到Engade_武器,然后转移到itemMatrix中时,它将丢失其单独的值,成为一个字符串。这就是我需要解决的问题,尽管这也确实有帮助。嗯,我不明白你需要什么,请你提出一个关于堆栈溢出的新问题好吗?
for item in items_in_existence:
    print(item['name'], item['rank'])