syntax errors and the check module command?

Reply

Join Date: Oct 2006
Posts: 71
Reputation: mruane is an unknown quantity at this point 
Solved Threads: 1
mruane mruane is offline Offline
Junior Poster in Training

syntax errors and the check module command?

 
0
  #1
Oct 21st, 2006
Alright, so far, what I think the problem begins with is not having a full understanding of syntax, and tabs versus spaces. I have my tab set to four spaces. When I run the following code, the Python GUI (the black ms-dos looking thing, right?) flashes for only a few seconds and the program kills itself.
I use Python 2.4.5 i believe, and just downloaded PyGame and Py2exe thinking they could further my ability to produce a decent first time program. Anyways, Many of you already know I am starting off by attempting to write a text based game. I think my only problem is syntax, but no matter how I realign the script, I still get an error. Sometimes in the same place, sometimes another.
Well, here is my script...The majority of it was adapted from other sources I found either on this site or elsewhere online.
  1. # this is supposed to be simple
  2. # I am trying to simulate combat
  3. # As easy as I can
  4. # "Untitled"
  5. # By Franki
  6. # You find the crystals to destroy the Solomon Terminal
  7. import random #!!!!! What does this do anyway?
  8. crystals = 0
  9. global gotCrystal_1
  10. global gotCrystal_2
  11. global gotCrystal_3
  12. global gotCrystal_4
  13.  
  14. gotCrystal_area1 = 0
  15. gotCrystal_area2 = 0
  16. gotCrystal_area3 = 0
  17. gotCrystal_area4 = 0
  18. gold = 0
  19. global gotGold_1
  20. global gotGold_2
  21. global gotGold_3
  22. global gotGold_4
  23. gotGold_area1 = 0
  24. gotGold_area2 = 0
  25. gotGold_area3 = 0
  26. gotgold_area4 = 0
  27. # I am using Classes to make monsters (I don't know why)
  28. class Monster(object): #!!!!! Is this right? I found it in a snippet?
  29. """Monster"""
  30. def __init__(self,name,level):
  31. self.name = name
  32. self.level = level
  33.  
  34. # The Monsters
  35. bad_hunter = Monster("Cavernian Hunter","1")
  36. bad_scout = Monster("Cavernian Scout","2")
  37. bad_warrior = Monster("Cavernian Warrior","3")
  38. bad_commander = Monster("Cavernian Troop Commander","4")
  39. def place_monster():
  40. # the integer 1 places Monster, all others place nothing
  41. MOnster_in = random.randrange(2)
  42. if Monster_in == (0 or 2):
  43. return 0
  44. Monster = random.randrange(4)
  45. if Monster == 0:
  46. Monster = bad_hunter
  47. elif Monster == 1:
  48. Monster = bad_scout
  49. elif Monster == 2:
  50. Monster = bad_warrior
  51. elif Monster == 3:
  52. Monster = bad_commander
  53. return Monster
  54.  
  55. # THE AREAS
  56. # Only four rooms (areas) in first mission
  57. #
  58. #------Mission One Map--------
  59. #===============================
  60. #| 4 | 2 | Room 3 has stairs going to upstairs study
  61. #| | | Room 1 has exit house/enter town
  62. #|-----------|--------------| Room 2 is start point. Has chest full of gold
  63. #| 3 | 1 | needed to begin game and buy weapons.
  64. #| | |
  65. #==============================
  66. #
  67. # Framework:
  68. # room# = Room(name,description,enemies_y_n?)
  69. # Next add the coordinates below the room creators
  70. # Keep organized
  71.  
  72. # This is the beginning of the game
  73. # It might be too long
  74. # I don't know how to give a back story
  75. # that gives enough information and isn't tediously long
  76. def help():
  77. print '''Here is a list of commands. This is not exclusive, new commands
  78. will come into availablitly as you progress through the game.
  79. Actions: open (object) look at (object), survey, tell (target), go (direction; N,E, S, W,)
  80. take (object), drop (object), throw (object), attack (target), avoid (target).
  81. EXAMPLES: look at (tree), tell (Mr. Rogers), go (E), take (gold), drop (sword),
  82. throw (knife), attack (hunter), avoid (hunter) open (chest).
  83. REMEMBER: Enter all commands in lower case letters.'''
  84. print
  85. def start():
  86. print '''It is the year 2115, over 100 years since the team of scientists
  87. discovered advanced foriegn schematics for an alternative energy source.
  88. Though the attempt to compress and harness the energy of fledging stars
  89. seemed to work rather well, slow and deadly atmospheric changes began to occur,
  90. until finally, mere years before the Sun lost 48% of it's energy, along with
  91. an estimated 5 out of 7 stars in our solar system losing 26.8% of their's,
  92. a great earthquake shook the Earth apart, revealing an underground civilization
  93. only 50 years behind Mankind in their technology.
  94. These underground dwellers, soon recognized as the long lost ancient man, the
  95. Neandrothal, weren't to pleased to have their world destroyed, along with their
  96. energy sources. Now, the Earth is a primitive world, and the time became known
  97. as The Second Dark Age. The sun no longer provided enough light to give us daytime,
  98. so we live in perpetual night.'''
  99. print
  100. def area1():
  101. global gold
  102. global crystals
  103. print " You have = ", gold "gold"
  104. print " You have = ", crystals "crystals"
  105. print ''' You are in your house, in the upstairs study. You hear shouts
  106. coming from the village outside. In the study there are STAIRS going down,
  107. there is a DESK against the far wall, and a CHEST.
  108. Type 'help' for a full list of commands. Your only available exit is DOWN.'''
  109. print
  110. prompt_ar1()
  111. def ar1_desc():
  112. print ''' The walls are made of logs fastened together with rope. Your DESK
  113. is cluttered with papers and books. The STAIRS going DOWN are your only exit. '''
  114. print
  115. prompt_ar1()
  116. def prompt_ar1():
  117. global gold
  118. global gotGold_area1
  119. global gold_found1
  120. prompt_a = raw_input("What will you do?").lower()
  121. try:
  122. if prompt_a == "go down":
  123. area2()
  124. elif prompt_a == "look at stairs":
  125. print "These stairs lead DOWN"
  126. elif prompt_a == "look at desk":
  127. print "Their is a DIARY on the desk"
  128. elif prompt_a == "take diary":
  129. print "You have taken the DIARY"
  130. elif prompt_a == "look at diary":
  131. print '''June 05, 2096,
  132. I have failed miserably. My project may very well be the end of the world.
  133. The creatures that we awoke from underground are attacking now, and some
  134. people started their own armies in an attempt to control what they see as
  135. as the best means to gain world power. But I fear there may not be a world to control
  136. very soon. I have seen odd Seismic activity in '
  137. -The entry is not finished. Their is blood stained on the page.'''
  138. elif prompt_a == "look at chest":
  139. print " There is an inscription on it. EMERG. EQUIP."
  140. elif prompt_a == "open chest":
  141. print "There are 500 GOLD. There is 1 DAGGER. There is 1 SHOTGUN."
  142. elif prompt_a == "take gold":
  143. gold=gold+500
  144. elif prompt_a == "take dagger":
  145. print "You have 1 dagger."
  146. elif prompt_a == "take shotgun":
  147. print "You have 1 shotgun."
  148. elif prompt_a == "survey":
  149. ar1_desc()
  150. elif prompt_a == "help":
  151. help()
  152. else:
  153. print "You cannot do that."
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 1,514
Reputation: Ene Uran has a spectacular aura about Ene Uran has a spectacular aura about 
Solved Threads: 168
Ene Uran's Avatar
Ene Uran Ene Uran is offline Offline
Posting Virtuoso

Re: syntax errors and the check module command?

 
0
  #2
Oct 21st, 2006
A first quick look revealed a number of things. Give attention to my comments marked with "!!!!". Here is code that should work, so you can improve on it:
  1. # this is supposed to be simple
  2. # I am trying to simulate combat
  3. # As easy as I can
  4. # "Untitled"
  5. # By Franki
  6. # You find the crystals to destroy the Solomon Terminal
  7.  
  8. import random # needed for random.randrange(2) or other random thingies
  9.  
  10. crystals = 0
  11.  
  12. """
  13. # declare global variables inside functions only!!!!
  14. global gotCrystal_1
  15. global gotCrystal_2
  16. global gotCrystal_3
  17. global gotCrystal_4
  18. global gotGold_1
  19. global gotGold_2
  20. global gotGold_3
  21. global gotGold_4
  22. """
  23.  
  24. # use False, later set to True, is more readable
  25. gotCrystal_area1 = False
  26. gotCrystal_area2 = False
  27. gotCrystal_area3 = False
  28. gotCrystal_area4 = False
  29.  
  30. gotGold_area1 = False
  31. gotGold_area2 = False
  32. gotGold_area3 = False
  33. gotgold_area4 = False
  34.  
  35. gold = 0
  36.  
  37. # I am using Classes to make monsters (I don't know why)
  38. # so you can later refer to as bad_warrior.level, should be 3
  39. # or bad_hunter.name which is 'Cavernian Hunter'
  40. # you can also define some action within the class
  41. class Monster(object):
  42. """Monster"""
  43. def __init__(self,name,level):
  44. self.name = name
  45. self.level = level
  46.  
  47. # The Monsters
  48. bad_hunter = Monster("Cavernian Hunter","1")
  49. bad_scout = Monster("Cavernian Scout","2")
  50. bad_warrior = Monster("Cavernian Warrior","3")
  51. bad_commander = Monster("Cavernian Troop Commander","4")
  52.  
  53. def place_monster():
  54. # the integer 1 places Monster, all others place nothing
  55. Monster_in = random.randrange(2) # replaced MOnster_in with Monster_in spelling!!!!!
  56. if Monster_in == (0 or 2):
  57. return 0
  58. Monster = random.randrange(4)
  59. if Monster == 0:
  60. Monster = bad_hunter
  61. elif Monster == 1:
  62. Monster = bad_scout
  63. elif Monster == 2:
  64. Monster = bad_warrior
  65. elif Monster == 3:
  66. Monster = bad_commander
  67. return Monster
  68.  
  69. # THE AREAS
  70. # Only four rooms (areas) in first mission
  71. #
  72. #------Mission One Map--------
  73. #===============================
  74. #| 4 | 2 | Room 3 has stairs going to upstairs study
  75. #| | | Room 1 has exit house/enter town
  76. #|-----------|--------------| Room 2 is start point. Has chest full of gold
  77. #| 3 | 1 | needed to begin game and buy weapons.
  78. #| | |
  79. #==============================
  80. #
  81. # Framework:
  82. # room# = Room(name,description,enemies_y_n?)
  83. # Next add the coordinates below the room creators
  84. # Keep organized
  85.  
  86. # This is the beginning of the game
  87. # It might be too long
  88. # I don't know how to give a back story
  89. # that gives enough information and isn't tediously long
  90.  
  91. def help(location):
  92. print '''Here is a list of commands. This is not exclusive, new commands
  93. will come into availablitly as you progress through the game.
  94. Actions: open (object) look at (object), survey, tell (target), go (direction; N,E, S, W,)
  95. take (object), drop (object), throw (object), attack (target), avoid (target).
  96. EXAMPLES: look at (tree), tell (Mr. Rogers), go (E), take (gold), drop (sword),
  97. throw (knife), attack (hunter), avoid (hunter) open (chest).
  98. REMEMBER: Enter all commands in lower case letters.'''
  99. print
  100. # help() should return to the area you used last!!!!
  101. location()
  102.  
  103. def start():
  104. print '''It is the year 2115, over 100 years since the team of scientists
  105. discovered advanced foriegn schematics for an alternative energy source.
  106. Though the attempt to compress and harness the energy of fledging stars
  107. seemed to work rather well, slow and deadly atmospheric changes began to occur,
  108. until finally, mere years before the Sun lost 48% of it's energy, along with
  109. an estimated 5 out of 7 stars in our solar system losing 26.8% of their's,
  110. a great earthquake shook the Earth apart, revealing an underground civilization
  111. only 50 years behind Mankind in their technology.
  112. These underground dwellers, soon recognized as the long lost ancient man, the
  113. Neandrothal, weren't to pleased to have their world destroyed, along with their
  114. energy sources. Now, the Earth is a primitive world, and the time became known
  115. as The Second Dark Age. The sun no longer provided enough light to give us daytime,
  116. so we live in perpetual night.'''
  117. print
  118.  
  119. def area1():
  120. global gold
  121. global crystals
  122. print " You have = ", gold, "gold" # added a , after gold!!!!
  123. print " You have = ", crystals, "crystals" # dito with crystal !!!!
  124. print ''' You are in your house, in the upstairs study. You hear shouts
  125. coming from the village outside. In the study there are STAIRS going down,
  126. there is a DESK against the far wall, and a CHEST.
  127. Type 'help' for a full list of commands. Your only available exit is DOWN.'''
  128. print
  129. prompt_ar1()
  130.  
  131. def ar1_desc():
  132. print ''' The walls are made of logs fastened together with rope. Your DESK
  133. is cluttered with papers and books. The STAIRS going DOWN are your only exit. '''
  134. print
  135. prompt_ar1()
  136.  
  137. def prompt_ar1():
  138. global gold
  139. global gotGold_area1
  140. global gold_found1
  141. prompt_a = raw_input("What will you do? ").lower()
  142. try:
  143. if prompt_a == "go down":
  144. area2()
  145. elif prompt_a == "look at stairs":
  146. print "These stairs lead DOWN"
  147. elif prompt_a == "look at desk":
  148. print "Their is a DIARY on the desk"
  149. elif prompt_a == "take diary":
  150. print "You have taken the DIARY"
  151. elif prompt_a == "look at diary":
  152. print '''June 05, 2096,
  153. I have failed miserably. My project may very well be the end of the world.
  154. The creatures that we awoke from underground are attacking now, and some
  155. people started their own armies in an attempt to control what they see as
  156. as the best means to gain world power. But I fear there may not be a world to control
  157. very soon. I have seen odd Seismic activity in '
  158. -The entry is not finished. Their is blood stained on the page.'''
  159. elif prompt_a == "look at chest":
  160. print " There is an inscription on it. EMERG. EQUIP."
  161. # once the gold is taken, there should be no more gold in the chest!!!!
  162. # dito for the dagger and the shotgun
  163. elif prompt_a == "open chest":
  164. if gotGold_area1 == False:
  165. print "There are 500 GOLD. There is 1 DAGGER. There is 1 SHOTGUN."
  166. else:
  167. print "There is 1 DAGGER. There is 1 SHOTGUN."
  168. # after the gold is taken set gotGold_area1 = True !!!!
  169. elif prompt_a == "take gold" and gotGold_area1 == False:
  170. gold = gold + 500
  171. print " You have = ", gold, "gold" # may want to inform player
  172. gotGold_area1 = True
  173. elif prompt_a == "take dagger":
  174. print "You have 1 dagger."
  175. elif prompt_a == "take shotgun":
  176. print "You have 1 shotgun."
  177. elif prompt_a == "survey":
  178. ar1_desc()
  179. elif prompt_a == "help":
  180. help(area1) # added location to return to from help!!!!
  181. else:
  182. print "You cannot do that."
  183. except: # try: has to have matching except: !!!!!!!!!!
  184. pass # pass for right now
  185. # you need to return to the "What will you do? " for more action
  186. # unless you are leaving the room
  187. prompt_ar1() # added this !!!!
  188.  
  189.  
  190. # get the action going ...
  191. start()
  192. area1()
drink her pretty
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 128
Reputation: LaMouche is on a distinguished road 
Solved Threads: 19
LaMouche's Avatar
LaMouche LaMouche is offline Offline
Junior Poster

Re: syntax errors and the check module command?

 
0
  #3
Oct 21st, 2006
Nice ideas. With Ene Uran's fixes, it's coming right along... here's a note.

  1. REMEMBER: Enter all commands in lower case letters.
You don't need this note in the help because you have solved this problem when you have them type in a command:


  1. prompt_a = raw_input("What will you do? ").lower()
The ".lower()" at the end of that line makes whatever they input lowercase.
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 71
Reputation: mruane is an unknown quantity at this point 
Solved Threads: 1
mruane mruane is offline Offline
Junior Poster in Training

Re: syntax errors and the check module command?

 
0
  #4
Oct 23rd, 2006
Thanks Ene Uran, I appreciate your taking the time to view and correct my coding. I hope it wasn't to tedious for you. To Lamouche, thanks, as you probably saw, I got most of my information on the coding from you. Just know I didn't merely copy and paste, I typed it all in myself, hoping it would give me an understanding of syntax and functions. Anyways, I hope you don't mind.
As for syntax, what is the best way to maintain/correct errors, and should I use spaces or tabs, and how many?
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 71
Reputation: mruane is an unknown quantity at this point 
Solved Threads: 1
mruane mruane is offline Offline
Junior Poster in Training

Re: syntax errors and the check module command?

 
0
  #5
Oct 23rd, 2006
I get the same old error message with the coding I recently added.
There is an error in your program.
Invalid syntax.

Here is the code:
  1. f area2():
  2. global gold
  3. global crystals #The error lies here I believe (global crystals)
  4. print " You have = ", gold, "gold" # added a , after gold!!!!
  5. print " You have = ", crystals, "crystals" # dito with crystal !!!!
  6. print ''' You are in your living room. There is a time worn sofa in the far corner,
  7. and a sleeping cat sprawls across the arm. There is a WINDOW above the sofa.
  8. Your exits are UP and WEST.'
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 128
Reputation: LaMouche is on a distinguished road 
Solved Threads: 19
LaMouche's Avatar
LaMouche LaMouche is offline Offline
Junior Poster

Re: syntax errors and the check module command?

 
0
  #6
Oct 23rd, 2006
I see two syntax errors. One is that first line should be def area2() the other is in this statement:

  1. print ''' You are in your living room. There is a time worn sofa in the far corner,
  2. and a sleeping cat sprawls across the arm. There is a WINDOW above the sofa.
  3. Your exits are UP and WEST.'

For a triple-quoted string, you need triple quotes at the end, too!

I am currently at school and have no python interpreter, so I can't double check whether it works or not. Hope that helped.
Last edited by LaMouche; Oct 23rd, 2006 at 3:10 pm.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 1,514
Reputation: Ene Uran has a spectacular aura about Ene Uran has a spectacular aura about 
Solved Threads: 168
Ene Uran's Avatar
Ene Uran Ene Uran is offline Offline
Posting Virtuoso

Re: syntax errors and the check module command?

 
0
  #7
Oct 23rd, 2006
Do all of us a favor and uses spaces for indentation!! The reason, different text editors have set different number of spaces for the tabs!!

Looks like the line that starts with global does not line up with the next line starting with print!

A Python statement block expects consistent spacing! I prefer 4 spaces. Use an editor made for Python coding and set the indentation option to "spaces only", also translate tabs to 4 spaces.

Also, please use code tags around your code, it will preserve the indents.
I will show the needed tags in reverse so they don't activate
at the end of your code add this tag [/code]
at the start of your code add tag [code]
Last edited by Ene Uran; Oct 23rd, 2006 at 3:28 pm.
drink her pretty
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 608
Reputation: jrcagle is on a distinguished road 
Solved Threads: 150
jrcagle jrcagle is offline Offline
Practically a Master Poster

Re: syntax errors and the check module command?

 
0
  #8
Oct 23rd, 2006
Also, if I'm debugging code that I want to run in a separate window, I first open it with IDLE and either Check Module (Run --> Check Module or Alt-X) or just Run it (F5). IDLE gives me *much* more info about my errors than just having my code start then halt with no message given!

I even do this with programs written for GUIs like Tkinter; technically, they aren't supposed to work, but I can usually nurse them along.

Jeff
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 71
Reputation: mruane is an unknown quantity at this point 
Solved Threads: 1
mruane mruane is offline Offline
Junior Poster in Training

Re: syntax errors and the check module command?

 
0
  #9
Oct 24th, 2006
Originally Posted by
Also, please use code tags around your code, it will preserve the indents.
I will show the needed tags in reverse so they don't activate
at the end of your code add this tag [/code
at the start of your code add tag [code]
Do you mean to do this within the Python shell or on the forum? I believe I used them on the forum. Wow, Python is really very picky, but I believe that to be a fair trade off for how pwerful it is.

I am attempting to write some combat codes, and am not sure how.
I was thinking of using random integers and true or false definitions combined with if, elif statements. Hope that makes sense. I just don't really know how to go about it.
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 3,959
Reputation: vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice 
Solved Threads: 918
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite

Re: syntax errors and the check module command?

 
0
  #10
Oct 25th, 2006
I think Ene added the code tags thing as a reminder so your indentation show properly in the forum posts. Thanks for using them so judiciously!

I have to think about the battle with a monster. You could use two lists of hitpoints, that you could shuffle at the start. One list is the sequence of your hit points and the other list is for the monster. They deduct from the strength points with each swing. Hopefully the monster will reach zero strength before you do!

Something like my_hitlist = [1, 0, 2, 0, 3, 2, 1, 1] where 0 is a miss, 1 is a mild blow, 2 starts to hurt and 3 is an ouch! Then you use random.shuffle(my_hitlist) to shuffle the list in place. Do the same with the monster_hitlist. That pits the two shuffled sequences against each other, each of them iterated in "I hit him" and "he hit me" for loops
Last edited by vegaseat; Oct 25th, 2006 at 4:20 am.
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the Python Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC