Problem using a dictionary [SOLVED]

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
User avatar
TrickWithAKnife
Eileen-Class Veteran
Posts: 1261
Joined: Fri Mar 16, 2012 11:38 am
Projects: Rika
Organization: Solo (for now)
IRC Nick: Trick
Location: Tokyo, Japan
Contact:

Problem using a dictionary [SOLVED]

#1 Post by TrickWithAKnife » Tue Jul 02, 2013 10:33 pm

I'm having problems with converting a short string to another short string using a dictionary. The full error message is at the bottom of the post.
It's confusing me, because it's copied from some existing code that works fine.

The value of destination_card_name is "a".
What I would expect to see from this is destination_card_name_in_hiragana should have the value "あ" at the end.

Code: Select all

# From a label:
    $ destination_card_name_in_hiragana = convert_romaji_to_kana(destination_card_name, hiragana_list) # Line 325


init python:
    def convert_romaji_to_kana(romaji, kana_list):
        keys = kana_list.keys();
        if romaji in keys:
            kana = dict[romaji] # Line 350
        return kana 
        
        
    hiragana_list = {
        "a" : "あ",
        "i" : "い",
        "u" : "う",
        "e" : "え",
        "o" : "お"}  
-- Full Traceback ------------------------------------------------------------

Full traceback:
File "D:\Renpy\renpy\execution.py", line 288, in run
node.execute()
File "D:\Renpy\renpy\ast.py", line 718, in execute
renpy.python.py_exec_bytecode(self.code.bytecode, self.hide, store=self.store)
File "D:\Renpy\renpy\python.py", line 1297, in py_exec_bytecode
exec bytecode in globals, locals
File "game/Flashcards.rpy", line 325, in <module>
$ destination_card_name_in_hiragana = convert_romaji_to_kana(destination_card_name, hiragana_list)
File "game/Flashcards.rpy", line 350, in convert_romaji_to_kana
kana = dict[romaji]
TypeError: 'type' object has no attribute '__getitem__'

Windows-7-6.1.7601-SP1
Ren'Py 6.15.6.372
A Ren'Py Game 0.0
Last edited by TrickWithAKnife on Tue Jul 02, 2013 11:16 pm, edited 1 time in total.
"We must teach them through the tools with which they are comfortable."
The #renpy IRC channel is a great place to chat with other devs. Due to the nature of IRC and timezone differences, people probably won't reply right away.

If you'd like to view or use any code from my VN PM me. All code is freely available without restriction, but also without warranty or (much) support.

RangerDanger
Regular
Posts: 45
Joined: Wed Jun 27, 2012 11:36 pm
Contact:

Re: Problem using a dictionary

#2 Post by RangerDanger » Tue Jul 02, 2013 10:55 pm

dict is a reserved word in python that refers to the python dictionary type.

So code like:

Code: Select all

dict[key]

will throw an exception.

You'll need to change line 350 so that you are looking up the key in a dictionary object, like kana_list.
Last edited by RangerDanger on Tue Jul 02, 2013 11:01 pm, edited 1 time in total.

User avatar
Anima
Veteran
Posts: 448
Joined: Wed Nov 18, 2009 11:17 am
Completed: Loren
Projects: PS2
Location: Germany
Contact:

Re: Problem using a dictionary

#3 Post by Anima » Tue Jul 02, 2013 11:00 pm

This is how you can do it. If the romanji is not in the list it will return None.

Code: Select all

init python:
    def convert_romaji_to_kana(romaji, kana_list):
        return kana_list.get(romanji)
Avatar created with this deviation by Crysa
Currently working on:
  • Winterwolves "Planet Stronghold 2" - RPG Framework: Phase III

User avatar
TrickWithAKnife
Eileen-Class Veteran
Posts: 1261
Joined: Fri Mar 16, 2012 11:38 am
Projects: Rika
Organization: Solo (for now)
IRC Nick: Trick
Location: Tokyo, Japan
Contact:

Re: Problem using a dictionary

#4 Post by TrickWithAKnife » Tue Jul 02, 2013 11:16 pm

It's working perfectly now. Thanks a lot.
"We must teach them through the tools with which they are comfortable."
The #renpy IRC channel is a great place to chat with other devs. Due to the nature of IRC and timezone differences, people probably won't reply right away.

If you'd like to view or use any code from my VN PM me. All code is freely available without restriction, but also without warranty or (much) support.

Post Reply

Who is online

Users browsing this forum: No registered users