| | |
syntax errors and the check module command?
![]() |
•
•
Join Date: Oct 2006
Posts: 71
Reputation:
Solved Threads: 1
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.
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.
Python Syntax (Toggle Plain Text)
# this is supposed to be simple # I am trying to simulate combat # As easy as I can # "Untitled" # By Franki # You find the crystals to destroy the Solomon Terminal import random #!!!!! What does this do anyway? crystals = 0 global gotCrystal_1 global gotCrystal_2 global gotCrystal_3 global gotCrystal_4 gotCrystal_area1 = 0 gotCrystal_area2 = 0 gotCrystal_area3 = 0 gotCrystal_area4 = 0 gold = 0 global gotGold_1 global gotGold_2 global gotGold_3 global gotGold_4 gotGold_area1 = 0 gotGold_area2 = 0 gotGold_area3 = 0 gotgold_area4 = 0 # I am using Classes to make monsters (I don't know why) class Monster(object): #!!!!! Is this right? I found it in a snippet? """Monster""" def __init__(self,name,level): self.name = name self.level = level # The Monsters bad_hunter = Monster("Cavernian Hunter","1") bad_scout = Monster("Cavernian Scout","2") bad_warrior = Monster("Cavernian Warrior","3") bad_commander = Monster("Cavernian Troop Commander","4") def place_monster(): # the integer 1 places Monster, all others place nothing MOnster_in = random.randrange(2) if Monster_in == (0 or 2): return 0 Monster = random.randrange(4) if Monster == 0: Monster = bad_hunter elif Monster == 1: Monster = bad_scout elif Monster == 2: Monster = bad_warrior elif Monster == 3: Monster = bad_commander return Monster # THE AREAS # Only four rooms (areas) in first mission # #------Mission One Map-------- #=============================== #| 4 | 2 | Room 3 has stairs going to upstairs study #| | | Room 1 has exit house/enter town #|-----------|--------------| Room 2 is start point. Has chest full of gold #| 3 | 1 | needed to begin game and buy weapons. #| | | #============================== # # Framework: # room# = Room(name,description,enemies_y_n?) # Next add the coordinates below the room creators # Keep organized # This is the beginning of the game # It might be too long # I don't know how to give a back story # that gives enough information and isn't tediously long def help(): print '''Here is a list of commands. This is not exclusive, new commands will come into availablitly as you progress through the game. Actions: open (object) look at (object), survey, tell (target), go (direction; N,E, S, W,) take (object), drop (object), throw (object), attack (target), avoid (target). EXAMPLES: look at (tree), tell (Mr. Rogers), go (E), take (gold), drop (sword), throw (knife), attack (hunter), avoid (hunter) open (chest). REMEMBER: Enter all commands in lower case letters.''' def start(): print '''It is the year 2115, over 100 years since the team of scientists discovered advanced foriegn schematics for an alternative energy source. Though the attempt to compress and harness the energy of fledging stars seemed to work rather well, slow and deadly atmospheric changes began to occur, until finally, mere years before the Sun lost 48% of it's energy, along with an estimated 5 out of 7 stars in our solar system losing 26.8% of their's, a great earthquake shook the Earth apart, revealing an underground civilization only 50 years behind Mankind in their technology. These underground dwellers, soon recognized as the long lost ancient man, the Neandrothal, weren't to pleased to have their world destroyed, along with their energy sources. Now, the Earth is a primitive world, and the time became known as The Second Dark Age. The sun no longer provided enough light to give us daytime, so we live in perpetual night.''' def area1(): global gold global crystals print " You have = ", gold "gold" print " You have = ", crystals "crystals" print ''' You are in your house, in the upstairs study. You hear shouts coming from the village outside. In the study there are STAIRS going down, there is a DESK against the far wall, and a CHEST. Type 'help' for a full list of commands. Your only available exit is DOWN.''' prompt_ar1() def ar1_desc(): print ''' The walls are made of logs fastened together with rope. Your DESK is cluttered with papers and books. The STAIRS going DOWN are your only exit. ''' prompt_ar1() def prompt_ar1(): global gold global gotGold_area1 global gold_found1 prompt_a = raw_input("What will you do?").lower() try: if prompt_a == "go down": area2() elif prompt_a == "look at stairs": print "These stairs lead DOWN" elif prompt_a == "look at desk": print "Their is a DIARY on the desk" elif prompt_a == "take diary": print "You have taken the DIARY" elif prompt_a == "look at diary": print '''June 05, 2096, I have failed miserably. My project may very well be the end of the world. The creatures that we awoke from underground are attacking now, and some people started their own armies in an attempt to control what they see as as the best means to gain world power. But I fear there may not be a world to control very soon. I have seen odd Seismic activity in ' -The entry is not finished. Their is blood stained on the page.''' elif prompt_a == "look at chest": print " There is an inscription on it. EMERG. EQUIP." elif prompt_a == "open chest": print "There are 500 GOLD. There is 1 DAGGER. There is 1 SHOTGUN." elif prompt_a == "take gold": gold=gold+500 elif prompt_a == "take dagger": print "You have 1 dagger." elif prompt_a == "take shotgun": print "You have 1 shotgun." elif prompt_a == "survey": ar1_desc() elif prompt_a == "help": help() else: print "You cannot do that."
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:
Python Syntax (Toggle Plain Text)
# this is supposed to be simple # I am trying to simulate combat # As easy as I can # "Untitled" # By Franki # You find the crystals to destroy the Solomon Terminal import random # needed for random.randrange(2) or other random thingies crystals = 0 """ # declare global variables inside functions only!!!! global gotCrystal_1 global gotCrystal_2 global gotCrystal_3 global gotCrystal_4 global gotGold_1 global gotGold_2 global gotGold_3 global gotGold_4 """ # use False, later set to True, is more readable gotCrystal_area1 = False gotCrystal_area2 = False gotCrystal_area3 = False gotCrystal_area4 = False gotGold_area1 = False gotGold_area2 = False gotGold_area3 = False gotgold_area4 = False gold = 0 # I am using Classes to make monsters (I don't know why) # so you can later refer to as bad_warrior.level, should be 3 # or bad_hunter.name which is 'Cavernian Hunter' # you can also define some action within the class class Monster(object): """Monster""" def __init__(self,name,level): self.name = name self.level = level # The Monsters bad_hunter = Monster("Cavernian Hunter","1") bad_scout = Monster("Cavernian Scout","2") bad_warrior = Monster("Cavernian Warrior","3") bad_commander = Monster("Cavernian Troop Commander","4") def place_monster(): # the integer 1 places Monster, all others place nothing Monster_in = random.randrange(2) # replaced MOnster_in with Monster_in spelling!!!!! if Monster_in == (0 or 2): return 0 Monster = random.randrange(4) if Monster == 0: Monster = bad_hunter elif Monster == 1: Monster = bad_scout elif Monster == 2: Monster = bad_warrior elif Monster == 3: Monster = bad_commander return Monster # THE AREAS # Only four rooms (areas) in first mission # #------Mission One Map-------- #=============================== #| 4 | 2 | Room 3 has stairs going to upstairs study #| | | Room 1 has exit house/enter town #|-----------|--------------| Room 2 is start point. Has chest full of gold #| 3 | 1 | needed to begin game and buy weapons. #| | | #============================== # # Framework: # room# = Room(name,description,enemies_y_n?) # Next add the coordinates below the room creators # Keep organized # This is the beginning of the game # It might be too long # I don't know how to give a back story # that gives enough information and isn't tediously long def help(location): print '''Here is a list of commands. This is not exclusive, new commands will come into availablitly as you progress through the game. Actions: open (object) look at (object), survey, tell (target), go (direction; N,E, S, W,) take (object), drop (object), throw (object), attack (target), avoid (target). EXAMPLES: look at (tree), tell (Mr. Rogers), go (E), take (gold), drop (sword), throw (knife), attack (hunter), avoid (hunter) open (chest). REMEMBER: Enter all commands in lower case letters.''' # help() should return to the area you used last!!!! location() def start(): print '''It is the year 2115, over 100 years since the team of scientists discovered advanced foriegn schematics for an alternative energy source. Though the attempt to compress and harness the energy of fledging stars seemed to work rather well, slow and deadly atmospheric changes began to occur, until finally, mere years before the Sun lost 48% of it's energy, along with an estimated 5 out of 7 stars in our solar system losing 26.8% of their's, a great earthquake shook the Earth apart, revealing an underground civilization only 50 years behind Mankind in their technology. These underground dwellers, soon recognized as the long lost ancient man, the Neandrothal, weren't to pleased to have their world destroyed, along with their energy sources. Now, the Earth is a primitive world, and the time became known as The Second Dark Age. The sun no longer provided enough light to give us daytime, so we live in perpetual night.''' def area1(): global gold global crystals print " You have = ", gold, "gold" # added a , after gold!!!! print " You have = ", crystals, "crystals" # dito with crystal !!!! print ''' You are in your house, in the upstairs study. You hear shouts coming from the village outside. In the study there are STAIRS going down, there is a DESK against the far wall, and a CHEST. Type 'help' for a full list of commands. Your only available exit is DOWN.''' prompt_ar1() def ar1_desc(): print ''' The walls are made of logs fastened together with rope. Your DESK is cluttered with papers and books. The STAIRS going DOWN are your only exit. ''' prompt_ar1() def prompt_ar1(): global gold global gotGold_area1 global gold_found1 prompt_a = raw_input("What will you do? ").lower() try: if prompt_a == "go down": area2() elif prompt_a == "look at stairs": print "These stairs lead DOWN" elif prompt_a == "look at desk": print "Their is a DIARY on the desk" elif prompt_a == "take diary": print "You have taken the DIARY" elif prompt_a == "look at diary": print '''June 05, 2096, I have failed miserably. My project may very well be the end of the world. The creatures that we awoke from underground are attacking now, and some people started their own armies in an attempt to control what they see as as the best means to gain world power. But I fear there may not be a world to control very soon. I have seen odd Seismic activity in ' -The entry is not finished. Their is blood stained on the page.''' elif prompt_a == "look at chest": print " There is an inscription on it. EMERG. EQUIP." # once the gold is taken, there should be no more gold in the chest!!!! # dito for the dagger and the shotgun elif prompt_a == "open chest": if gotGold_area1 == False: print "There are 500 GOLD. There is 1 DAGGER. There is 1 SHOTGUN." else: print "There is 1 DAGGER. There is 1 SHOTGUN." # after the gold is taken set gotGold_area1 = True !!!! elif prompt_a == "take gold" and gotGold_area1 == False: gold = gold + 500 print " You have = ", gold, "gold" # may want to inform player gotGold_area1 = True elif prompt_a == "take dagger": print "You have 1 dagger." elif prompt_a == "take shotgun": print "You have 1 shotgun." elif prompt_a == "survey": ar1_desc() elif prompt_a == "help": help(area1) # added location to return to from help!!!! else: print "You cannot do that." except: # try: has to have matching except: !!!!!!!!!! pass # pass for right now # you need to return to the "What will you do? " for more action # unless you are leaving the room prompt_ar1() # added this !!!! # get the action going ... start() area1()
drink her pretty
Nice ideas. With Ene Uran's fixes, it's coming right along... here's a note.
You don't need this note in the help because you have solved this problem when you have them type in a command:
The ".lower()" at the end of that line makes whatever they input lowercase.
Python Syntax (Toggle Plain Text)
REMEMBER: Enter all commands in lower case letters.
Python Syntax (Toggle Plain Text)
prompt_a = raw_input("What will you do? ").lower()
•
•
Join Date: Oct 2006
Posts: 71
Reputation:
Solved Threads: 1
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?
As for syntax, what is the best way to maintain/correct errors, and should I use spaces or tabs, and how many?
•
•
Join Date: Oct 2006
Posts: 71
Reputation:
Solved Threads: 1
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:
There is an error in your program.
Invalid syntax.
Here is the code:
Python Syntax (Toggle Plain Text)
f area2(): global gold global crystals #The error lies here I believe (global crystals) print " You have = ", gold, "gold" # added a , after gold!!!! print " You have = ", crystals, "crystals" # dito with crystal !!!! print ''' You are in your living room. There is a time worn sofa in the far corner, and a sleeping cat sprawls across the arm. There is a WINDOW above the sofa. Your exits are UP and WEST.'
I see two syntax errors. One is that first line should be
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.
def area2() the other is in this statement: Python Syntax (Toggle Plain Text)
print ''' You are in your living room. There is a time worn sofa in the far corner, and a sleeping cat sprawls across the arm. There is a WINDOW above the sofa. 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.
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]
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
•
•
Join Date: Jul 2006
Posts: 608
Reputation:
Solved Threads: 150
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
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
•
•
Join Date: Oct 2006
Posts: 71
Reputation:
Solved Threads: 1
•
•
•
•
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
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]
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.
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
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!
![]() |
Other Threads in the Python Forum
- Previous Thread: xml and p
- Next Thread: Plotting a Line Curve (Python)
| Thread Tools | Search this Thread |
accessdenied advanced aliased argv beginner bits calling casino change changecolor clear command convert corners count csv cturtle cursor def definedlines dictionary digital dynamic dynamically enter event events external file float format frange function google homework i/o iframe import input jaunty java keyboard lapse line linux list lists loop matching microphone mouse multiple newb number numbers obexftp output parameters parsing path port prime programming projects py py2exe pygame pyopengl python random rational raw_input recursion return scrolledtext signal simple singleton skinning sprite string strings tails text threading time tlapse tuple tutorial ubuntu unicode urllib urllib2 valueerror variable voip web-scrape whileloop word wxpython






