473,387 Members | 1,463 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Invalid Syntax Error for an If Statement in a Function

For some odd reason my code was all working fine until I made a few changes which broke almost everything in the function. I've since removed said changes but it's still broken. It's saying the colon in this section is Invalid Syntax. In fact it's saying the same for every if statement as well as some of the comparison operators.

Expand|Select|Wrap|Line Numbers
  1. if difficulty == "e":
  2.   number_enemies -= 1
Oct 3 '13 #1

✓ answered by stdq

Thanks! I managed to run the code after a few changes. I saved it in txt because I couldn't upload it here in .py format. I began selecting all text and then pressing shift + tab twice; then, there was also a missing parentheses at the end of 11. This file, in .py format, ran ok. Sometimes, the error can be in a line other than the one indicated. Please let me know if you have any doubts. By the way, I changed the file encoding to UTF-8 due to an error that I got in here, but it may have been unnecessary. Please let me know if you still have any problems.

Maybe the only change you need to make is to add the missing parenthesis to your code.

5 5394
stdq
94 64KB
Hi, can you post all your code?
Oct 3 '13 #2
The whole function is quite long, but sure. It's supposed to create the enemies in a text game I'm working on.

Expand|Select|Wrap|Line Numbers
  1. # Randomly generates enemies
  2. def create_enemies(difficulty, mode, current_location, battles):
  3.  
  4.   # Clears enemies from the last battle
  5.   enemies = []
  6.  
  7.   # List of possible names for the enemies to use
  8.   names = ["Aiden", "James", "Sam", "Aidan", "Brad", "Pavle", "Fuda", "David", "Andrew", "Harry", "Callum", "Tomasz", "Bazli", "Georgia", "Sheldon", "Legolas", "Aragorn", "Gimli", "Boromir", "Faramir", "Éomer", "Éowyn", "Treebeard", "Celeborn", "Galadrial", "Radagast", "Gandalf", "Merry", "Pippin", "Frodo", "Sam", "Bilbo", "Azog", "Gorkil", "Sharku", "Shagrat", "Gorbag", "Lurtz", "Tom Bombadil", "Goldberry"]
  9.  
  10.   # Randomly decides how many enemies there will be
  11.   number_enemies = int(random.randint(2,5)
  12.  
  13.   # 1 less enemy in Easy difficulty
  14.   if difficulty == "e": 
  15.     number_enemies -= 1
  16.   # 1 more enemy in Hard difficulty
  17.   elif difficulty == "h":
  18.     number_enemies += 1
  19.   # 2 more enemies in Legendary difficulty
  20.   elif difficulty == "l":
  21.     number_enemies += 2
  22.  
  23.   # The more battles you've had, the more enemies there will be
  24.   number_enemies += int((battles/100)*30)
  25.  
  26.   # There will always be at least 2 enemies
  27.   if not (number_enemies >= 2):
  28.     number_enemies = 2
  29.  
  30.   #Enemies are different at the boss battles
  31.   if mode == 2 and current_location.evil_final == True:
  32.     enemies = [Troll("Cave Troll"), Uruk("Uruk"), Wizard("Sauron"), Orc("Orc"), WargRider("Warg Rider"), Easterling("Easterling"), Goblin("Goblin 1"), Goblin("Goblin 2"), Goblin("Goblin 3")]
  33.   elif mode == 1 and current_location.good_final == True:
  34.     enemies = [Wizard("Gandalf"), Elf("Legolas"), Dwarf("Gimli"), Ranger("Aragorn"), Human("Boromir"), Hobbit("Frodo"), Hobbit("Sam"), Hobbit("Merry"), Hobbit("Pippin")]
  35.   else:
  36.     for z in range(number_enemies):
  37.       if mode == 1:
  38.         # Determines the class of the enemy
  39.         # 1/4 Goblin, 1/4 Orc, 1/8 Uruk, 1/8 Easterling, 3/24 Warg Rider, 2/24, Wizard, 1/24 Troll
  40.         enemy_class = random.randint(1, 24)
  41.         if enemy_class in range(1,6):
  42.           enemies.append(Goblin(names[random.randint(1,(len(names)-1))]))
  43.         if enemy_class in range(7,12):
  44.           enemies.append(Orc(names[random.randint(1,(len(names)-1))]))
  45.         if enemy_class in range(13,15):
  46.           enemies.append(Uruk(names[random.randint(1,(len(names)-1))]))
  47.         if enemy_class in range(16,18):
  48.           enemies.append(Easterling(names[random.randint(1,(len(names)-1))]))
  49.         if enemy_class in range(19,21):
  50.           enemies.append(WargRider(names[random.randint(1,(len(names)-1))]))
  51.         if enemy_class in range(22,23):
  52.           enemies.append(Wizard(names[random.randint(1,(len(names)-1))]))
  53.         if enemy_class == 24:
  54.           enemies.append(Troll(names[random.randint(1,(len(names)-1))]))
  55.  
  56.       if mode == 2:
  57.         # Determines the class of the enemy
  58.         # 1/4 Hobbit, 1/4 Human, 1/8 Elf, 1/8 Dwarf, 3/24 Ranger, 2/24 Wizard, 1/24 Ent
  59.         enemy_class = random.randint(1, 24)
  60.         if enemy_class in range(1,6):
  61.           enemies.append(Hobbit(names[random.randint(1,(len(names)-1))]))
  62.         if enemy_class in range(7,12):
  63.           enemies.append(Human(names[random.randint(1,(len(names)-1))]))
  64.         if enemy_class in range(13,15):
  65.           enemies.append(Elf(names[random.randint(1,(len(names)-1))]))
  66.         if enemy_class in range(16,18):
  67.           enemies.append(Dwarf(names[random.randint(1,(len(names)-1))]))
  68.         if enemy_class in range(19,21):
  69.           enemies.append(Ranger(names[random.randint(1,(len(names)-1))]))
  70.         if enemy_class in range(22,23):
  71.           enemies.append(Wizard(names[random.randint(1,(len(names)-1))]))
  72.         if enemy_class == 24:
  73.           enemies.append(Ent(names[random.randint(1,(len(names)-1))]))
  74.  
  75.   return enemies
Oct 3 '13 #3
stdq
94 64KB
Thanks! I managed to run the code after a few changes. I saved it in txt because I couldn't upload it here in .py format. I began selecting all text and then pressing shift + tab twice; then, there was also a missing parentheses at the end of 11. This file, in .py format, ran ok. Sometimes, the error can be in a line other than the one indicated. Please let me know if you have any doubts. By the way, I changed the file encoding to UTF-8 due to an error that I got in here, but it may have been unnecessary. Please let me know if you still have any problems.

Maybe the only change you need to make is to add the missing parenthesis to your code.
Attached Files
File Type: txt forum.txt (3.7 KB, 380 views)
Oct 3 '13 #4
That missing parentheses... I put it there and suddenly everything works perfectly. Thank you so much!
Oct 3 '13 #5
stdq
94 64KB
You're welcome!

Best,
stdq
Oct 3 '13 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

29
by: shank | last post by:
1) I'm getting this error: Syntax error (missing operator) in query expression on the below statement. Can I get some advice. 2) I searched ASPFAQ and came up blank. Where can find the "rules"...
4
by: evan.cooch | last post by:
Greetings. Suppose I have some function called "CheckIt" - some function to validate form data before submitting it to e CGI script. Pretend the name of the form is "TheForm". If I use the...
4
by: Bradley Kite | last post by:
Hi all. I'm trying to diagnose/solve a problem with internet explorer, whereby sometimes the form submits, and other times IE produces an 'invalid syntax' error. First, I have a form, and...
0
by: Mark Phanides | last post by:
My ASP.NET application intermittantly (but always at same point) redirects to the 'Invalid Syntax Error' web page for some unknown reason. I've created a ASP.NET application written in VB.NET with...
2
by: py | last post by:
Hi, I am running python 2.4.2 on win xp pro. I have the WMI module from Tim Golden (http://tgolden.sc.sabren.com/python/wmi.html). I have some code which does this... MyScript.py...
10
by: ronrsr | last post by:
no matter where I place this imported file,the statement after it in the main program gets a syntax error, regardless of the syntax. I think I may have changed something in this file, but I'm...
7
by: Hexer | last post by:
Hi and good day to all...I am new to the C language and whenever I tried to run the program I made, it won't because it says it has an "expression syntax error in function main"..what I don't get is...
10
by: jonathanemil | last post by:
Hello, I am a 1st semester Computer Science student in a Python class. Our current assignment calls for us to read a list from a file, create a 2-dimensional list from the file, and check to see...
3
by: kakarot vegeta | last post by:
There 's an invalid syntax error but i don't know what's wrong with it. the error says Invalid syntax: print variation_string import itertools variations = itertools.product('ab',...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.