Python Forum
Unable to unpack at line 184 - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Homework (https://python-forum.io/forum-9.html)
+--- Thread: Unable to unpack at line 184 (/thread-33468.html)



Unable to unpack at line 184 - Nitil - Apr-27-2021

Good day to all. Im new to the forums and want to excel in Python.

This a simple code for an RPG game with the above error in line 184. Would like to know where I have gone wrong to avoid such mistakes in the future.

Game class - https://pastebin.com/8TWH5dBT
Magic class - https://pastebin.com/Am4Ff2Zh
Inventory class - https://pastebin.com/6bVGSsHD

Main file - https://pastebin.com/StPDRJ20


RE: Unable to unpack at line 184 - Axel_Erfurt - Apr-27-2021

It's not a good idea to use pastebin.


RE: Unable to unpack at line 184 - ibreeden - Apr-28-2021

    def choose_enemy_spell(self):
        magic_choice = random.randrange(0, len(self.magic))
        spell = self.magic[magic_choice]
        magic_dmg = spell.generate_damage()

        pct = self.hp / self.maxhp * 100

        if self.mp < spell.cost or spell.type == "white" and pct > 50:
            self.choose_enemy_spell()
        else:
            return spell, magic_dmg
In one case you return nothing, in the other case you return (spell, magic_dmg). It is the first case that will give you the error.


RE: Unable to unpack at line 184 - popejose - Apr-28-2021

Somewhere in your code there must be line that looks something like

spell, dmg = choose_enemy_spell()
Python is telling us that it is "unable to unpack". Consider what spell and dmg variables would be in the case where nothing is returned.

/regards


RE: Unable to unpack at line 184 - Nitil - May-01-2021

Thank you all for the support.
I made the said changes and the code is working fine.

I'll make sure to ask the questions in the right format from the next time. Please pardon me