Home > database >  I get the TypeError: 'int' object is not iterable, but not really about the error
I get the TypeError: 'int' object is not iterable, but not really about the error

Time:12-24

My question isn't really about why I get the error, because I already found something of a solution, but this one wont work for me. So I tried this

game

import random
import listAdventureGame
p_aT = 'no armor'

armor_type = listsAdventureGame.aT
armor_durability = listsAdventureGame.aD
armor_points = listsAdventureGame.aP

game_data = dict(zip(str(armor_type[p_aT]), str(armor_durability[p_aT])))
print(game_data)
p_aP = armor_points[p_aT]
p_aT = random.choice(list(game_data))
p_aD = game_data[p_aT]

print("Armor type:", p_aT)
print("Armor durability:", p_aD)
print("Armor point(s):", p_aP) 

listAdventureGame

aT = {'no armor': 'nothing',
      'light': ('leather armor', 'normal clothes', 'ranger armor', 'animal skins'),
      'medium': ('chain mail armor', 'fireproof cloak', 'tank armor', 'fighter armor', 'copper armor'),
      'heavy': ('plate armor', 'dragon scale armor', 'rune armor')}

aD = {'no armor': 0,
      'light': (100, 75, 90, 105),
      'medium': (130, 110, 145, 115, 120),
      'heavy': (185, 200, 190)}

aP = {'no armor': 100,
      'light': 5,
      'medium': 3,
      'heavy': 2}

but then the output is something like

Armor type: n
Armor durability: 0
Armor points: 100

and I want Armor type: nothing and when I change the p_aT it still works, I'm going to make this a function somewhere in the future.

I removed the str() in the zip()

game_data = dict(zip(armor_type[p_aT], armor_durability[p_aT]))

I'm really just a beginner and a visual learner, so if you're able to do it by showing the code.

Thx in advance

CodePudding user response:

As you said, 'when I change the p_aT it still works' (I imagine you mean it works with other armor types): if so, find the difference between 'no armor' and other armor types; it lies in the first 2 dictionaries, where you use tuples except for the 'no armor' key. The fix is then easy to devise:

aT = {'no armor': ('nothing',), ...
aD = {'no armor': (0,), ...

the game_data statement will now return the correct result for 'no armor'.

CodePudding user response:

just replace game_data = dict(zip(armor_type[p_aT], armor_durability[p_aT])) the with this line of code. game_data = dict(zip([armor_type[p_aT]], [armor_durability[p_aT]]))

maybe the zip function only try to match the first and second item character by character. seems awesome but the code above actually works out.

  • Related