The code I provided just repeatedly ask the user to enter an age until it's an integer (or a floating point number, but it would be truncated by the integer casting).
Ah ha.
I did wonder about the "while True:"
But I've never done python, nevermind to the level you clearly have. I saw the potential loop, but not how "print e" or "continue" would remain within it. I suppose I could have tested it, but I wasn't invested enough in problem beyond a simple reply.
(it's from the summertime saga code, changed must point to a callback, and default to any variable, all those keyword arguments are optional, note the allow keyword)
I'm going to guess that if he's struggling with "convert string to integer", callbacks might be a slightly further along in the manual than he's read so far. It's certainly beyond me... so I'm simply going to put my head in my hands and slowly rock back and forth while mumbling for a while.
I have used "input={charlist}" as an parameter for renpy.input before. The rest is completely over my head.
On beta testing, I found out that the code that I thought was working was actually broken. Here's the code:
Python:
python:
shaneage = int(renpy.input("Shane is ___ years old? (Default: 18)"))
if not shaneage:
shaneage = int(18)
Whenever someone just hit return (selecting the default), the game threw an error. In the second conditional, removing the int() from around 18 (which was already an integer) got rid of the error.
Okay. I suppose if it works... then it works. I would try entering something really odd, like "dog" instead of a number as a final test.
But the "
try:" / "
except:" construct in
@Dogeek 's post is the answer to "what if it generates an error?".
What "
try" does is say "do this command, but if you get an error... check the error against the list of errors defined by the "
except" clause. (if you don't get an error, it just carries on anyway).
So in this case, he's saying "try to return the integer of the string you entered". Then "if you get an error while converting that string to an integer (the '
except ValueError')... then do these commands instead".
The "
while True:"... and I'm guessing the "
continue"... keep it within a loop, repeatedly asking the player to keep entering a number until a valid one is accepted.
On top of all that, he's defining the whole thing as a new function (the '
def int_input(string=""):').
So instead of:
Python:
python:
shaneage = int(renpy.input("Shane is ___ years old? (Default: 18)"))
if not shaneage:
shaneage = int(18)
You'd define the new function somewhere in your code . If you're using a single "script.rpy", then convention says probably near the top. Then use the new function further down...
Something like:
Python:
def int_input(string=""):
while True:
x = input(string)
try:
return int(x)
except ValueError as e:
print(e)
continue
# <<< loads of other code >>>
label start:
# <<< more code >>>
shaneage = int_input("Shane is ___ years old? (Default: 18)")
# <<< even more code >>>
In this case, you don't need to do all the "
not shaneage:" type tests, because it's already handled by the "
try:" logic within the new function.
Again, I haven't tested this code... it may work, just as I've written it... but if not... it'll need some tweaking.