Re: [Tutor] Here is code, no link for my previous question

2013-01-07 Thread Alan Gauld

On 07/01/13 02:07, Jack Little wrote:

Here is the code, my error is below the code in itallics


That does not look like a normal Python error report. How are you 
running this? If you are using an IDE it may be giving non standard 
messages. It is usually better to run your code from a command line and 
then send us the full error traceback. It contains a lot of useful 
information.


Although in this case the error is sufficiently explicit that we can
pinpoint the error without it.


global ammo1
global ammo2
global ammo3
global health
global tech_parts
global exp
global radio_parts


These do absolutely nothing, get rid of them.



def simpstart():
   global ammo
   global health
   global tech_parts
   global radio_parts
print "You awake in a haze. A crate,a door and a radio."
g1 = raw_input("Which do you choose  ")



Remember that indentation is significant in Python.
By moving the indentation of print out to the margin you have ended your 
simpstart() function. As a result simpstart() does absolutely

nothing.


if g1 == "CRATE" or g1 == "Crate" or g1 == "crate":


The usual way to do this is use the lower() method of strings:

if g1.lower() == 'crate':


 print "There is a pack of ammo,some food and an odd microchip"
 ammo1=ammo1 + 6

>  health=health + 10
>  tech_parts=tech_parts + 1

You can use the += shortcut to save typing

ammo1 += 6
health += 10
tech_parts += 1


elif g2 == "NORTH" or g2 == "North" or g2 == "north":

   def path_1pt1():
 print "This is where it all started. Freedom Tower. A biotech firm
called Aesthos Biotechnology. Based here."


You are now trying to define a new function inside the elif block.
That's legal Python and you can do it if you really want to, but its 
very unusual style. Normally functions are defined at outer level and 
called when needed.







   def path_1pt2():
 print "There is a barbed wire topped fence, about 7 feet high.
There is a building."
 print "The weather is picking up, and the wind is getting fast and
violent."
 p5 = raw_input("Stay outside and risk it or check the inside of the
building  ")
 if p5 == "Stay" or p5 == "STAY" or p5 == "stay":
 print "The wind and snow picked up. You got frostbite"
 return path_1pt1()
 elif p5 == "CHECK" or p5 == "Check" or p5 == "check":
 print "There is a group of survivors huddled in a corner"
 print  """Survivor 1: Who are you?
   Me: Does that matter?
   Survivor 2: You aren't of 'em Protectors are ya?
   Me: The what?
   Survivor 1: They go around killin' people who don't
comply with their rules and live in New Quebec"
Me:Huh"""
 exp=exp+200
 health=health+50
 ammo1=ammo1+29
 p6 = raw_input("Do you wish to take a quest or continue the story?  ")


p6 is defined inside the path_1pt2 function but not returned.


   if p6 == "QUEST" or p6 == "Quest" or p6 == "quest":


This line is not part of the function but tests p6. That won;t work.
I suspect its an indentation error.
indentation is very important in Python.


   quest1()


Since the quest() function is not defined in your code I assume you are 
importing it from some other module?




   elif p6 == "CONTINUE STORY" or p6 == "Continue story" or p6 ==



   p9 = raw_input("Go east or west  ")
   if p9 == "EAST" or p9 == "east" or p9 == "East":
 def GasStation():
   if p10.lower== "approach kindly":
   print """Me: Hey Guys!
   ammo1=ammo1-6


And this is the end of your GasStation() function because the
next line is not indented...


else:
 print"They murdered you!"
 return GasStation()
 ammo1=ammo1-6


And the above is probably where your error occurs since return is indeed 
outside of a function.


Incidentally if it were in a function the ammo line would never
get executed since it is after the return, so it is pointless.



else:
   return path1_pt2()
   "A snowstorm swept through, killing you."


Same here, the return is outside a function.
And the following string is meaningless


Here is the error:
There's an error in your program:
***'return' outside function(g.py,line 179)


As I say, please run the program in an environment that prints proper 
python error messages and send the full error text in future.


You need to go away and read about indentation,
function definitions and namespaces.
They are the biggest problems in your code at present.

Also please use bigger indentation size, a single character makes it 
really hard to read and spot which indent lines up with what.

Also these very long control structures likewise make it hard to
look up and see what aligns with what above. (If with else/elif etc)
That all makes your code much harder to maintain and debug.

HTH

--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/

___
Tutor maillist  -  Tut

Re: [Tutor] Decorators: Are they good for checking inputs and outputs?

2013-01-07 Thread Steven D'Aprano

On 06/01/13 23:30, DoanVietTrungAtGmail wrote:

Dear tutors

After much reading and head-scratching, I think the basic idea of
decorators has now clicked for me. I am a beginner in programming and in
Python, but I want to eventually develop a serious system. To spend most of
my time on developing the ideas and building the code, I need to test my
code fairly well but spend as little time doing so as possible. Therefore,
I am looking ahead and thinking of using decorators extensively.

Specifically, for every function I will write, I don't want to have to
write code to check that arguments passed to it are of the permitted
number, type, and range, then code to deal with those errors that can be
dealt with. This is what I hope: Once I have collected or written all the
necessary decorators, from then on I'll just routinely decorate each
function with a whole bunch of decorators relevant to it.



That is one use of decorators. It is not the only use.

To really understand decorators well, you have to understand the idea of
*higher order functions*. Most people get to understand the idea of a
function: you pass some arguments, and it returns a result:

def plus1(a):
return a + 1

def plus2(a):
return a + 2

def plus3(a):  # this is getting tedious...
return a + 3


Higher order functions take that one step higher: instead of returning a
result (a number, a string, some other value) they return a function!

def plus_factory(value):
# Create an inner function.
def plus(a):
return a + value  # value comes from the outer function
# Change the name of the inner function, so it looks nicer.
plus.__name__ = "plus%d" % value  # plus1, plus2, ... plus999
# Return the inner function.
return plus

Now I can do:

plus4 = plus_factory(4)
plus100 = plus_factory(100)
plus_a_lot = plus_factory(1234567890)


print plus4(16)  # prints 20
print plus100(16)  # prints 116


So now we have seen 1st order functions, which take simple arguments
like numbers or strings and return a new number or string, and 2nd
order functions, which take a simple argument and returns a function.
Now we look at 3rd order functions that take a function as an argument
and return a new function!


def noisy_function(func):
name = func.__name__
def inner(x):
print "calling function %s with argument %r" % (name, x)
return func(x)
return inner


Now I can do this:


plus4 = noisy_function(plus4)

print plus4(16)

If I haven't make any mistakes, this will print two lines:

calling function plus4 with argument 16
20


Decorators are a way of easily using 3rd order functions. There are
many, many different uses for them.



However, it seems not many people think like I do. In the discussions I
have read on StackOverflow and elsewhere, few people use decorators for the
above purposes, and those who do, don't seem to do it extensively. If
decorators are truly as useful as I think they are for the above purposes,
surely they would be used more enthusiastically, more extensively, by many
more people.


This is Python. We're not really keen on unnecessary type checking. There are
better, or at least different, ways of getting the same effect.



So, my first question to the tutors is: Am I giving decorators undeservedly
high expectations for the above purposes (ie. checking inputs&  outputs,
and dealing with some problems therein)? Are there perhaps traps down the
line, invisible to me at the moment, which make decorators not as useful as
I hope?


Decorators are awesome!

But using decorators for type checking, meh. That's sometimes useful, but more
often it is just limiting and annoying. This is Python, not C, and it is best
to learn Python styles of coding rather that trying to write C or Java in
Python.



Second, if decorators are actually suitable for the above purposes, then
where can I find repositories of decorators? (I am aware of
the PythonDecoratorLibrary - PythonInfo Wiki, the link is not working now,
but did a few days ago)


I'm not aware of any specialist "decorator libraries", but many, many libraries
include their own decorators which they use for their own purposes.


--
Steven
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor