Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
SyntaxError: invalid syntax
#7
For checking if one thing is equal to one of two other things, you can use or:

if myPlayer.job == 'warrior' or myPlayer.job == 'mighty warrior':
However, it is generally simpler to use the 'in' operator:

if myPlayer.job in ('warrior', 'mighty warrior'):
Now, you can also use a dictionary:

HP_BY_JOB = {'warrior': 100, 'mighty warrior': 100, 'wizard': 50, 'mighty wizard': 50}
player_hp = HP_BY_JOB[my_player.job]
You could have a similar one for MP. That makes the code a lot simpler when you have a lot of jobs. Another thing you could do is have each job store all of it's base stats:

JOB_STATS = {'warrior': {'hp': 100, 'mp': 20}, 'wizard': {'hp': 50, 'mp': 120}, ...}
my_player.stats = JOB_STATS[my_player.job].copy()
That makes them easier to assign to the player, but it does make them a little harder to get from the player, because my_player.hp is now my_player.stats['hp']. You could use setattr to get around this:

JOB_STATS = {'warrior': {'hp': 100, 'mp': 20}, 'wizard': {'hp': 50, 'mp': 120}, ...}
for stat, value in JOB_STATS[my_player.job].items():
    setattr(my_player, stat, value)
Now it's just my_player.hp again. Just be careful with setattr that there isn't a key in the dictionary that is going to overwrite some important attribute of my_player.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Messages In This Thread
SyntaxError: invalid syntax - by Kanashi - Nov-22-2019, 10:58 PM
RE: SyntaxError: invalid syntax - by ichabod801 - Nov-22-2019, 11:02 PM
RE: SyntaxError: invalid syntax - by Kanashi - Nov-22-2019, 11:35 PM
RE: SyntaxError: invalid syntax - by ichabod801 - Nov-22-2019, 11:50 PM
RE: SyntaxError: invalid syntax - by Kanashi - Nov-23-2019, 04:48 AM
RE: SyntaxError: invalid syntax - by ichabod801 - Nov-23-2019, 03:13 PM
RE: SyntaxError: invalid syntax - by ichabod801 - Nov-23-2019, 03:27 PM
RE: SyntaxError: invalid syntax - by Kanashi - Nov-24-2019, 05:59 AM
RE: SyntaxError: invalid syntax - by ichabod801 - Nov-24-2019, 01:39 PM
RE: SyntaxError: invalid syntax - by Kanashi - Nov-24-2019, 08:47 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  print(data) is suddenly invalid syntax db042190 6 3,715 Jun-14-2023, 02:55 PM
Last Post: deanhystad
  SyntaxError: invalid syntax ?? korenron 15 10,335 Jan-25-2022, 11:46 AM
Last Post: korenron
  Invalid syntax with an f-string Mark17 7 17,473 Jan-14-2022, 04:44 PM
Last Post: Mark17
  invalid syntax in my class CompleteNewb 2 3,528 Dec-13-2021, 09:39 AM
Last Post: Larz60+
Exclamation Invalid syntax error(Predict Ethereum Price) lulu43366 2 4,484 Sep-24-2021, 01:24 PM
Last Post: lulu43366
  Unexplained Invalid syntax Error cybertooth 5 5,858 Aug-02-2021, 10:05 AM
Last Post: cybertooth
  [split] SyntaxError: invalid syntax Code_X 3 3,932 May-04-2021, 05:15 PM
Last Post: Yoriz
  Invalid syntax error - need help fixing calgk01 3 4,734 Feb-23-2021, 08:41 PM
Last Post: nilamo
  Invalid syntax using conditionals if - else jperezqu 1 3,092 Jan-13-2021, 07:32 PM
Last Post: bowlofred
  invalid syntax in line 5. Help Asadzangibaloch 2 3,361 Dec-10-2020, 04:26 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020