Invalid Syntax SyntaxError: What did I do wrong now?

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
facadepapergirl
Regular
Posts: 46
Joined: Thu Jan 31, 2013 7:27 pm
Projects: Pokemon: Final Evolution; Galactic Lust Crisis
Contact:

Invalid Syntax SyntaxError: What did I do wrong now?

#1 Post by facadepapergirl »

I'm trying to do a simple exp system in my game, where defeating the enemy will reward the player exp when they win. When exp is over 99 (So, at least at 100), they'll level up. I thought that I did it right, but as I'm still learning, it's no surprise that I messed up again. Here is the exp - leveling up code:

Code: Select all

$ level = 1
$ exp = 0

if exp >= 99 and <299:
    $ level = 2
if exp >= 299 and <599:
    $ level = 3
if exp >= 599 and <999:
    $ level = 4
if exp >= 999 and <1499:
    $ level = 5
if exp >= 1499 and <2099:
    $ level = 6
if exp >= 2099 and <2799:
    $ level = 7
if exp >= 2799 and <3599:
    $ level = 8
if exp >= 3599 and <4499:
    $ level = 9
if exp >= 4499 and <5499:
    $ level = 10
if exp >= 5499 and <6599:
    $ level = 11
if exp >= 6599 and <7799:
    $ level = 12
if exp >= 7799 and <9199:
    $ level = 13
if exp >= 9199 and <= 15999:
    $ level = 14
if exp >= 15999:
    $ level = 15
And if someone can help me with my renpy.random.randint problem here, I'd appreciate it. It's really bringing my game to a halt, as the battle and random encounter system relies on this.

jw2pfd
Regular
Posts: 87
Joined: Tue Sep 18, 2012 9:55 pm
Location: DFW, TX, USA
Contact:

Re: Invalid Syntax SyntaxError: What did I do wrong now?

#2 Post by jw2pfd »

The syntax should be:

Code: Select all

    if exp >= 99 and exp < 299:
And this is true for every line. So 'exp' needs to be on the right-hand side of every statement.

Code: Select all

                                        
    $ level = 1
    $ exp = 0

    if exp >= 99 and exp < 299:
        $ level = 2
    if exp >= 299 and exp < 599:
        $ level = 3
    if exp >= 599 and exp < 999:
        $ level = 4
    if exp >= 999 and exp < 1499:
        $ level = 5
    if exp >= 1499 and exp < 2099:
        $ level = 6
    if exp >= 2099 and exp < 2799:
        $ level = 7
    if exp >= 2799 and exp < 3599:
        $ level = 8
    if exp >= 3599 and exp < 4499:
        $ level = 9
    if exp >= 4499 and exp < 5499:
        $ level = 10
    if exp >= 5499 and exp < 6599:
        $ level = 11
    if exp >= 6599 and exp < 7799:
        $ level = 12
    if exp >= 7799 and exp < 9199:
        $ level = 13
    if exp >= 9199 and exp <= 15999:
        $ level = 14
    if exp >= 15999:
        $ level = 15
When using a conditional joined by 'and', there needs to a complete statement on both sides. if (statement 1) and (statement 2). For the following line of code that you originally had:

Code: Select all

if exp >= 99 and < 299:
It looks at 'exp >= 99' and that's a complete and syntactically correct statement that can evaluate properly. It looks at the right-hand side as ' < 299' and that is incorrect syntax. The code does not assume that you still want to use the same variable from earlier in the statement and therefore you need to explicitly use it each time.

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

Re: Invalid Syntax SyntaxError: What did I do wrong now?

#3 Post by Anima »

You can also slim the code down by using elif statements. Or even more with a loop and a formula.

Code: Select all

$ next_level = 1
$ next_level_xp = 100
while exp >= next_level_xp:
    $ next_level += 1
    $ next_level_xp += next_level*100
$ level = next_level
Avatar created with this deviation by Crysa
Currently working on:
  • Winterwolves "Planet Stronghold 2" - RPG Framework: Phase III

User avatar
facadepapergirl
Regular
Posts: 46
Joined: Thu Jan 31, 2013 7:27 pm
Projects: Pokemon: Final Evolution; Galactic Lust Crisis
Contact:

Re: Invalid Syntax SyntaxError: What did I do wrong now?

#4 Post by facadepapergirl »

Thank you both! I shall try to keep this in mind, jw2pfd, as I shall no doubt use it again. Anima, what did you mean by elif statements? I've always seen it as an else statement when there are a lot of those statements, but I'm a noob, so I use a lot of things that I don't understand. The Ren'Py documentation has a lot of info, but not enough examples or really technical language.

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

Re: Invalid Syntax SyntaxError: What did I do wrong now?

#5 Post by Anima »

You can do the same thing with this structure:

Code: Select all

if exp > 15999:
    $ level = 15
elif exp > 9199:
    $ level = 14
elif exp > 7799:
    $ level = 13
...
As you see you only need one test instead of two. It's also faster since it stops when the right level is reached.
The elif clauses are only elevated if the preceding if or elif clause was false. You can also keep the current order but need to change the comparison to lesser than.

Code: Select all

if exp < 100:
    $level = 1
elif exp < 300:
    $level = 2
elif exp < 600:
    $level = 3
...
You could also use a direct formula to calculate the level but it involves several type casts and a square root in this case.
Avatar created with this deviation by Crysa
Currently working on:
  • Winterwolves "Planet Stronghold 2" - RPG Framework: Phase III

Elmiwisa
Veteran
Posts: 476
Joined: Sun Jul 21, 2013 8:08 am
Contact:

Re: Invalid Syntax SyntaxError: What did I do wrong now?

#6 Post by Elmiwisa »

elif is just short hand for else follow by if. For example:

Code: Select all

elif exp<299:
is the same as:

Code: Select all

else:
    if exp<299:
If you just use else followed by if you would have to do an extra indentation, making the code look rather ugly when you have a lot of them stacking on top of each other. elif allow you to avoid that.

Also, you can use

Code: Select all

if 99<=exp<299:
as a short hand for:

Code: Select all

if exp>=99 and exp<299:
just like how they do it in maths books.

Post Reply

Who is online

Users browsing this forum: No registered users