Coercing to Unicode: Error when including int as input

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
DesertFox
Regular
Posts: 196
Joined: Sun Jul 28, 2013 1:29 pm
Completed: Over The Hills And Far Away
Projects: My Little Dictator
Organization: WarGirl Games
Contact:

Coercing to Unicode: Error when including int as input

#1 Post by DesertFox » Sat May 23, 2015 2:09 pm

Playing around with some stats that the player can enter. Start by getting them to enter their current weight in order to work out their goal weight. RenPy seems to think goal_weight should return a string rather than an integer and I'm not certain how to change this. There are also some issues with the first if current_weight > 100 in that it will accept any number whether or not it's below 100. Does renpy.input convert what the player enters? Does it remain a string rather than acknowledge an integer?

Code: Select all

label enter_weight:    
    
    $ difference = 10
    $ current_weight = renpy.input("What is your current weight?", allow='0123456789', exclude='qwertyuiopasdfghjklzxcvbnm,./;', length=3)
    
    if current_weight > 100:
        "Okay, so your current weight is [current_weight]."
    elif current_weight < 100:
        "Please enter a correct number!"
        jump enter_weight
    else:
        "Please enter a correct number!"
        jump enter_weight
    
    "Do you want to gain weight or lose weight?"
    
    menu:
        "Gain weight":
            $ gain_weight = True
            $ goal_weight = current_weight + difference
        "Lose weight":
            $ gain_weight = False
            $ goal_weight = current_weight - difference
            
    e "So your current weight is [current_weight] and your goal weight is [goal_weight]."

Code: Select all

While running game code:
  File "game/script.rpy", line 38, in script
    $ goal_weight = current_weight + difference
  File "game/script.rpy", line 38, in <module>
    $ goal_weight = current_weight + difference
TypeError: coercing to Unicode: need string or buffer, int found

User avatar
xela
Lemma-Class Veteran
Posts: 2481
Joined: Sun Sep 18, 2011 10:13 am
Contact:

Re: Coercing to Unicode: Error when including int as input

#2 Post by xela » Sat May 23, 2015 2:20 pm

Code: Select all

    $ difference = 10
    $ current_weight = renpy.input("What is your current weight?", allow='0123456789', exclude='qwertyuiopasdfghjklzxcvbnm,./;', length=3)
    $ current_weight = int(current_weight)
Try this.
Like what we're doing? Support us at:
Image

User avatar
DesertFox
Regular
Posts: 196
Joined: Sun Jul 28, 2013 1:29 pm
Completed: Over The Hills And Far Away
Projects: My Little Dictator
Organization: WarGirl Games
Contact:

Re: Coercing to Unicode: Error when including int as input

#3 Post by DesertFox » Sat May 23, 2015 2:44 pm

That works great!

Just a quick ask - Will that goal_weight remain at its initial value or will it continue to change as current_weight changes? So if the player can continue to input their current_weight and change that value... Will I need to alter goal_weight to a persistent variable?

For instance, later on I have this, that either chastises the player for improving on their current_weight or congratulates them.

Code: Select all

if gain_weight:
        if current_weight < (goal_weight - 10):
            e "Oh dear, you've got a way to go still!"
        else:
            e "Hmm, not too bad! We can do this!"
    else:
        if current_weight > (goal_weight + 10):
            e "Oh dear, you've got a way to go still!"
        else:
            e "Hmm, not too bad! We can do this!"
        
    e "Cool!"

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

Re: Coercing to Unicode: Error when including int as input

#4 Post by philat » Sat May 23, 2015 2:56 pm

goal_weight will remain static.

User avatar
DesertFox
Regular
Posts: 196
Joined: Sun Jul 28, 2013 1:29 pm
Completed: Over The Hills And Far Away
Projects: My Little Dictator
Organization: WarGirl Games
Contact:

Re: Coercing to Unicode: Error when including int as input

#5 Post by DesertFox » Sat May 23, 2015 4:27 pm

Awesome!

If I want to give the player the option to enter a decimal such as 180.5 rather than 180, do I use $ current_weight = float(current_weight) instead?

Code: Select all

$ current_weight = renpy.input("What is your current weight?", allow='0123456789', exclude='qwertyuiopasdfghjklzxcvbnm,./;', length=3)

User avatar
xela
Lemma-Class Veteran
Posts: 2481
Joined: Sun Sep 18, 2011 10:13 am
Contact:

Re: Coercing to Unicode: Error when including int as input

#6 Post by xela » Sat May 23, 2015 4:42 pm

Sure thing.
Like what we're doing? Support us at:
Image

User avatar
DesertFox
Regular
Posts: 196
Joined: Sun Jul 28, 2013 1:29 pm
Completed: Over The Hills And Far Away
Projects: My Little Dictator
Organization: WarGirl Games
Contact:

Re: Coercing to Unicode: Error when including int as input

#7 Post by DesertFox » Sat May 23, 2015 5:12 pm

Is there a certain expression to limit them to five characters to enter, but to ensure that after three characters, they have to enter a decimal point? So they can enter 180.0 but they can't enter 18000?

User avatar
xela
Lemma-Class Veteran
Posts: 2481
Joined: Sun Sep 18, 2011 10:13 am
Contact:

Re: Coercing to Unicode: Error when including int as input

#8 Post by xela » Sat May 23, 2015 11:42 pm

Prolly not... but you can always analyze the input or create some form of a pattern.
Like what we're doing? Support us at:
Image

User avatar
xavimat
Eileen-Class Veteran
Posts: 1458
Joined: Sat Feb 25, 2012 8:45 pm
Completed: Yeshua, Jesus Life, Cops&Robbers
Projects: Fear&Love, unknown
Organization: Pilgrim Creations
Github: xavi-mat
itch: pilgrimcreations
Location: Spain
Contact:

Re: Coercing to Unicode: Error when including int as input

#9 Post by xavimat » Sun May 24, 2015 12:38 pm

Just two notes:
- Using "allow" in renpy.input() is enough, there is no need to "exclude" (it's automatically excluded everything that is not "allowed")
- If you want decimals, you need to allow the "."

Code: Select all

renpy.input("What is your current weight?", allow='0123456789.', length=5)
Comunidad Ren'Py en español: ¡Únete a nuestro Discord!
Rhaier Kingdom A Ren'Py Multiplayer Adventure Visual Novel.
Cops&Robbers A two-player experiment | Fear&Love Why can't we say I love you?
Honest Critique (Avatar made with Chibi Maker by ~gen8)

User avatar
DesertFox
Regular
Posts: 196
Joined: Sun Jul 28, 2013 1:29 pm
Completed: Over The Hills And Far Away
Projects: My Little Dictator
Organization: WarGirl Games
Contact:

Re: Coercing to Unicode: Error when including int as input

#10 Post by DesertFox » Sun May 24, 2015 2:05 pm

@xavimat - Noted

@xela - Perhaps if there were two separate inputs for the int number and the decimal? So two boxes within a screen, next to one another. Then perhaps there's a way to append the int number with the decimal somehow...

User avatar
PyTom
Ren'Py Creator
Posts: 15893
Joined: Mon Feb 02, 2004 10:58 am
Completed: Moonlight Walks
Projects: Ren'Py
IRC Nick: renpytom
Github: renpytom
itch: renpytom
Location: Kings Park, NY
Contact:

Re: Coercing to Unicode: Error when including int as input

#11 Post by PyTom » Sun May 24, 2015 2:37 pm

I'd just check for an exception when coercing the result to a float.

Code: Select all

label ask_weight:
    $ weight = input("How fat are you?")

    python:
        try:
            weight = float(weight) 
        except:
            renpy.jump("bad_weight")

    jump good_weight
    
label bad_weight:

     v "Let's try that again. Enter a number this time."
     jump ask_weight


label good_weight:
     if weight >= 300:
          v "Just my type."

...
Supporting creators since 2004
(When was the last time you backed up your game?)
"Do good work." - Virgil Ivan "Gus" Grissom
"Silly and fun things are important." - Elon Musk
Software > Drama • https://www.patreon.com/renpytom

User avatar
DesertFox
Regular
Posts: 196
Joined: Sun Jul 28, 2013 1:29 pm
Completed: Over The Hills And Far Away
Projects: My Little Dictator
Organization: WarGirl Games
Contact:

Re: Coercing to Unicode: Error when including int as input

#12 Post by DesertFox » Sun May 24, 2015 8:52 pm

Code: Select all

label good_weight:
     if weight >= 300:
          v "Just my type."
I think we've all just learned something about PyTom's type...

User avatar
PyTom
Ren'Py Creator
Posts: 15893
Joined: Mon Feb 02, 2004 10:58 am
Completed: Moonlight Walks
Projects: Ren'Py
IRC Nick: renpytom
Github: renpytom
itch: renpytom
Location: Kings Park, NY
Contact:

Re: Coercing to Unicode: Error when including int as input

#13 Post by PyTom » Sun May 24, 2015 9:43 pm

I'm gonna call a foul there. Ren'Py is python based - we're not big on type checking.

(And I was assuming this was a character, not me. And.... enjoy your free code example.)
Supporting creators since 2004
(When was the last time you backed up your game?)
"Do good work." - Virgil Ivan "Gus" Grissom
"Silly and fun things are important." - Elon Musk
Software > Drama • https://www.patreon.com/renpytom

User avatar
DesertFox
Regular
Posts: 196
Joined: Sun Jul 28, 2013 1:29 pm
Completed: Over The Hills And Far Away
Projects: My Little Dictator
Organization: WarGirl Games
Contact:

Re: Coercing to Unicode: Error when including int as input

#14 Post by DesertFox » Sun May 24, 2015 10:27 pm

PyTom wrote:I'm gonna call a foul there. Ren'Py is python based - we're not big on type checking.

(And I was assuming this was a character, not me. And.... enjoy your free code example.)
Haha, just a joke :P

(And thank you!)

Post Reply

Who is online

Users browsing this forum: Google [Bot]