[solved]Error when trying to receive a key from value

Discuss how to use the Ren'Py engine to create visual novels and story-based games. New releases are announced in this section.
Forum rules
This is the right place for Ren'Py help. Please ask one question per thread, use a descriptive subject like 'NotFound error in option.rpy' , and include all the relevant information - especially any relevant code and traceback messages. Use the code tag to format scripts.
Post Reply
Message
Author
abysswatcher
Regular
Posts: 42
Joined: Sun Apr 12, 2020 11:50 pm
Projects: To-Live:The Struggle
Organization: Youyu de Shijie(憂鬱的世界)
Github: LuYifeng112
itch: https://luyifeng.itc
Location: New Zealand
Contact:

[solved]Error when trying to receive a key from value

#1 Post by abysswatcher »

I have been trying to get the max value from a dictionary and then getting the key for max value. Every method I try brings up the same issue:

Code: Select all

I'm sorry, but an uncaught exception occurred.

While running game code:
  File "game/TO_LIVE_SCRIPT/ERA ONE/1937 DAY ONE[7-9-37]/_1937_sino_japanese_war_bookmark.rpy", line 5, in script
    "test fasces"
TypeError: unsupported operand type(s) for %: 'NoneType' and 'TagQuotingDict'

-- Full Traceback ------------------------------------------------------------

Full traceback:
  File "game/TO_LIVE_SCRIPT/ERA ONE/1937 DAY ONE[7-9-37]/_1937_sino_japanese_war_bookmark.rpy", line 5, in script
    "test fasces"
  File "E:\renpy\renpy-6.99.12.4-sdk\renpy\ast.py", line 708, in execute
    renpy.exports.say(who, what, *args, **kwargs)
  File "E:\renpy\renpy-6.99.12.4-sdk\renpy\exports.py", line 1334, in say
    what = what % tag_quoting_dict
TypeError: unsupported operand type(s) for %: 'NoneType' and 'TagQuotingDict'

Windows-8-6.2.9200
Ren'Py 7.3.5.606
To Live 0.0.82
Mon Jul 27 13:53:44 2020
I wonder if this is an issue with renpy or something else that won't allow the retrieval of a key.
For context here is the code:

Code: Select all

def GetMaxPol(self):
            return max(self.pol.iteritems(), key=operator.itemgetter(1))[0]   
this is the dict:

Code: Select all

define FangIdeologyStats = {
    'fascism':1,
    'communism':0,
    'centrism':0,
    'anarchism':0,
    'conservatism':0,
    'corporatism':0,
    'liberalism':0,
    'libertarianism':0,
    'moralism':0,
    'nationalism':0,
    'progressivism':0
}
Finally the usage of the code where the error occurs:

Code: Select all

if fang.GetMaxPol() == "fascism":
    "testing for max ideology"
Last edited by abysswatcher on Wed Jul 29, 2020 5:50 am, edited 1 time in total.
The goal of the revolution is to achieve the people's rights, but during the course of the revolution, we must stress military power - and the two are mutually contradictory.
-Sun Yat-sen
"Become a Visual Novel writer they said, it will be fun" (little did they know they were right)

philat
Eileen-Class Veteran
Posts: 1912
Joined: Wed Dec 04, 2013 12:33 pm
Contact:

Re: Error when trying to receive a key from value

#2 Post by philat »

Honestly stack overflow is better for python questions. I googled stack overflow for python max dictionary key and found the following works fine (obvs simplified -- and obvs with a random return if there are ties):

Code: Select all

default testdict = {
    'fascism':1,
    'communism':0,
    'centrism':0,
    'anarchism':0,
    'conservatism':0,
    'corporatism':0,
    'liberalism':0,
    'libertarianism':0,
    'moralism':0,
    'nationalism':0,
    'progressivism':0
}

init python:
    def GetMaxPol(d):
        return max(d, key=lambda key: d[key])

label start:
    "test"

    $ temp = GetMaxPol(testdict)
    "[temp]"
    $ testdict["centrism"] += 1
    jump start

Post Reply

Who is online

Users browsing this forum: Bing [Bot]