Ren'Py Need help with dictionary

winter0s

Member
Aug 5, 2016
187
1,175
I was working on coding in Ren'Py with a friend (we both are rookies at this). I have attached the piece of code in question below. If I enter any of the values listed in that dictionary, the argument "$ curr = [x for x, y in currencies.items() if curr in y]" returns the correct key. However, if call [curr] it gives me [u'selected key'], for example - I type in dollar at the input screen, instead of returning American currency, it returns [u'American currency'] when I call using [curr]. (Screenshot attached with the error marked.)

How do I get rid of the [u''] and just display American currency?

Code:
default currencies = {
"American currency" : ["dollar", "dollars", "cent", "cents", "usd"],
"British currency" : ["pound", "pounds", "shilling", "shillings"],
}

label currency:
    $ curr = renpy.input("Which currency do you use?")
    $ curr = curr.strip().lower()
    $ currtemp = curr # a temporary variable for currency that is not listed in the dict above. User entered currency will be used.
    if curr == "":
        $ curr = "Other Currency" # if user does not enter any currency name
    else:
        $ curr = [x for x, y in currencies.items() if curr in y]
        if not curr:
            $ curr = currtemp
    jump currency_conf

label currency_conf:
    menu:
        "Your currency is [curr]. Is that right?"
        "Yes":
            "You clicked YES!"
            jump endgame
        "No":
            jump currency
Clipboard01.jpg
 

anne O'nymous

I'm not grumpy, I'm just coded that way.
Modder
Donor
Respected User
Jun 10, 2017
10,369
15,284
How do I get rid of the [u''] and just display American currency?
There's two points here.

Firstly the u"...". This, you can't and anyway don't need to get rid of it.
In Python, strings can be prefixed by a letter that will defined how the content will be interpreted, and that's what you get here. The "u" say that the string is coded in Unicode, but it's purely internal.

Secondly, the [...]. Well, no secret here, it's the way lists are represented...

In the end, you get exactly what you asked Ren'py to display : the content of the list named curr.
You have the [...] saying that it's a list, the u"..." saying that its first, and only, entry is an Unicode string, and finally what you expected to see, the content of the string, "American currency".


The problem come way before this, in the way you get the value of curr.

The syntax you used, [x for x in whatever if condition] and more globally all the [whatever for whatever] is designed to create a list. It happen that there can be only one value as result of the computation, but it doesn't mean that suddenly Python will return something else than a list.

The best way to solve this is simply to change the code this way :
Code:
    else:
        $ curr = [x for x, y in currencies.items() if curr in y]
        if curr:
            $ curr = curr[0]
        else:
            $ curr = currtemp
Therefore, to get what you thought you add, the string stored inside the list you created.
 
  • Like
Reactions: winter0s

winter0s

Member
Aug 5, 2016
187
1,175
The syntax you used, [x for x in whatever if condition] and more globally all the [whatever for whatever] is designed to create a list. It happen that there can be only one value as result of the computation, but it doesn't mean that suddenly Python will return something else than a list.
Oh yeah, that makes sense. I am basically saving it as a list that contains only one item.


Code:
    else:
        $ curr = [x for x, y in currencies.items() if curr in y]
        if curr:
            $ curr = curr[0]
        else:
            $ curr = currtemp
I tried your fix, but now the problem is it reads the value as curr. It returns "Your currency is curr. Is that right?" instead of pulling the actual item.:unsure:

Edit: It worked just fine! Thank you AON
 
Last edited: